Commit 3687f491 authored by Steven杜宇's avatar Steven杜宇

// mod: 验证码输入框

parent f1522165
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
04F526C72B3BB94200FC6CE6 /* YHSMSCodeInputView.m in Sources */ = {isa = PBXBuildFile; fileRef = 04F526C32B3BB94200FC6CE6 /* YHSMSCodeInputView.m */; }; 04F526C72B3BB94200FC6CE6 /* YHSMSCodeInputView.m in Sources */ = {isa = PBXBuildFile; fileRef = 04F526C32B3BB94200FC6CE6 /* YHSMSCodeInputView.m */; };
04F526C82B3BB94200FC6CE6 /* YHSMSCodeView.m in Sources */ = {isa = PBXBuildFile; fileRef = 04F526C42B3BB94200FC6CE6 /* YHSMSCodeView.m */; }; 04F526C82B3BB94200FC6CE6 /* YHSMSCodeView.m in Sources */ = {isa = PBXBuildFile; fileRef = 04F526C42B3BB94200FC6CE6 /* YHSMSCodeView.m */; };
04F526CA2B3E92E200FC6CE6 /* Algorithm.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F526C92B3E92E200FC6CE6 /* Algorithm.swift */; }; 04F526CA2B3E92E200FC6CE6 /* Algorithm.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F526C92B3E92E200FC6CE6 /* Algorithm.swift */; };
04F526CC2B3E962400FC6CE6 /* YNSmsCodeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F526CB2B3E962400FC6CE6 /* YNSmsCodeView.swift */; }; 04F526CC2B3E962400FC6CE6 /* YHSmsCodeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F526CB2B3E962400FC6CE6 /* YHSmsCodeView.swift */; };
04F526CE2B3EA80300FC6CE6 /* YHSmsCodeInputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F526CD2B3EA80300FC6CE6 /* YHSmsCodeInputView.swift */; }; 04F526CE2B3EA80300FC6CE6 /* YHSmsCodeInputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F526CD2B3EA80300FC6CE6 /* YHSmsCodeInputView.swift */; };
04F526D02B3ECAE700FC6CE6 /* YHValidateCodeInputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F526CF2B3ECAE700FC6CE6 /* YHValidateCodeInputView.swift */; }; 04F526D02B3ECAE700FC6CE6 /* YHValidateCodeInputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F526CF2B3ECAE700FC6CE6 /* YHValidateCodeInputView.swift */; };
04F526E52B3ECC6F00FC6CE6 /* UIApplication+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F526D82B3ECC6F00FC6CE6 /* UIApplication+Extension.swift */; }; 04F526E52B3ECC6F00FC6CE6 /* UIApplication+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F526D82B3ECC6F00FC6CE6 /* UIApplication+Extension.swift */; };
...@@ -91,7 +91,7 @@ ...@@ -91,7 +91,7 @@
04F526C52B3BB94200FC6CE6 /* YHSMSCodeInputView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YHSMSCodeInputView.h; sourceTree = "<group>"; }; 04F526C52B3BB94200FC6CE6 /* YHSMSCodeInputView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YHSMSCodeInputView.h; sourceTree = "<group>"; };
04F526C62B3BB94200FC6CE6 /* YHSMSCodeView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YHSMSCodeView.h; sourceTree = "<group>"; }; 04F526C62B3BB94200FC6CE6 /* YHSMSCodeView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YHSMSCodeView.h; sourceTree = "<group>"; };
04F526C92B3E92E200FC6CE6 /* Algorithm.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Algorithm.swift; sourceTree = "<group>"; }; 04F526C92B3E92E200FC6CE6 /* Algorithm.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Algorithm.swift; sourceTree = "<group>"; };
04F526CB2B3E962400FC6CE6 /* YNSmsCodeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YNSmsCodeView.swift; sourceTree = "<group>"; }; 04F526CB2B3E962400FC6CE6 /* YHSmsCodeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHSmsCodeView.swift; sourceTree = "<group>"; };
04F526CD2B3EA80300FC6CE6 /* YHSmsCodeInputView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHSmsCodeInputView.swift; sourceTree = "<group>"; }; 04F526CD2B3EA80300FC6CE6 /* YHSmsCodeInputView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHSmsCodeInputView.swift; sourceTree = "<group>"; };
04F526CF2B3ECAE700FC6CE6 /* YHValidateCodeInputView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YHValidateCodeInputView.swift; sourceTree = "<group>"; }; 04F526CF2B3ECAE700FC6CE6 /* YHValidateCodeInputView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YHValidateCodeInputView.swift; sourceTree = "<group>"; };
04F526D82B3ECC6F00FC6CE6 /* UIApplication+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIApplication+Extension.swift"; sourceTree = "<group>"; }; 04F526D82B3ECC6F00FC6CE6 /* UIApplication+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIApplication+Extension.swift"; sourceTree = "<group>"; };
...@@ -202,7 +202,7 @@ ...@@ -202,7 +202,7 @@
04F526C42B3BB94200FC6CE6 /* YHSMSCodeView.m */, 04F526C42B3BB94200FC6CE6 /* YHSMSCodeView.m */,
04F526C52B3BB94200FC6CE6 /* YHSMSCodeInputView.h */, 04F526C52B3BB94200FC6CE6 /* YHSMSCodeInputView.h */,
04F526C62B3BB94200FC6CE6 /* YHSMSCodeView.h */, 04F526C62B3BB94200FC6CE6 /* YHSMSCodeView.h */,
04F526CB2B3E962400FC6CE6 /* YNSmsCodeView.swift */, 04F526CB2B3E962400FC6CE6 /* YHSmsCodeView.swift */,
04F526CD2B3EA80300FC6CE6 /* YHSmsCodeInputView.swift */, 04F526CD2B3EA80300FC6CE6 /* YHSmsCodeInputView.swift */,
); );
path = "验证码输入框"; path = "验证码输入框";
...@@ -713,7 +713,7 @@ ...@@ -713,7 +713,7 @@
A52DF24D2B33177F006618D6 /* BsHUDWariningView.swift in Sources */, A52DF24D2B33177F006618D6 /* BsHUDWariningView.swift in Sources */,
A52DF0C32B330A20006618D6 /* YhCacheTool.swift in Sources */, A52DF0C32B330A20006618D6 /* YhCacheTool.swift in Sources */,
A52DF24A2B33177F006618D6 /* BsHUDContainerView.swift in Sources */, A52DF24A2B33177F006618D6 /* BsHUDContainerView.swift in Sources */,
04F526CC2B3E962400FC6CE6 /* YNSmsCodeView.swift in Sources */, 04F526CC2B3E962400FC6CE6 /* YHSmsCodeView.swift in Sources */,
04F526ED2B3ECC6F00FC6CE6 /* String+Extension.swift in Sources */, 04F526ED2B3ECC6F00FC6CE6 /* String+Extension.swift in Sources */,
A52DF0BD2B330A20006618D6 /* YHBaseUrlManager.swift in Sources */, A52DF0BD2B330A20006618D6 /* YHBaseUrlManager.swift in Sources */,
A5009DD82B3D72FD00168BB1 /* NetBaseModel.swift in Sources */, A5009DD82B3D72FD00168BB1 /* NetBaseModel.swift in Sources */,
......
...@@ -15,9 +15,9 @@ struct YHSmsCodeViewConfig { ...@@ -15,9 +15,9 @@ struct YHSmsCodeViewConfig {
var cursorColor : UIColor? = .black var cursorColor : UIColor? = .black
var font: UIFont? = .systemFont(ofSize: 20) var font: UIFont? = .systemFont(ofSize: 20)
// 展示输入框个数 // 展示输入框个数
var count : Int = 3 var count : Int = 6
var width : Double = 60.0 var width : Double = 40.0
var height : Double = 60.0 var height : Double = 50.0
// 输入框之间间距 // 输入框之间间距
var gap:Double = 8 var gap:Double = 8
} }
...@@ -26,15 +26,15 @@ let codeViewBaseTag = 9527 ...@@ -26,15 +26,15 @@ let codeViewBaseTag = 9527
class YHSmsCodeInputView: UIView { class YHSmsCodeInputView: UIView {
var smsCode: String? var complete: ((String) ->Void)?
var config: YHSmsCodeViewConfig = YHSmsCodeViewConfig() var config: YHSmsCodeViewConfig = YHSmsCodeViewConfig()
// 此输入框并不显示,只是接收输入 // 此输入框并不显示,只是接收输入
lazy var textField: UITextField = { lazy var textField: UITextField = {
let textField = UITextField() let textField = UITextField()
textField.backgroundColor = .clear textField.backgroundColor = .red
textField.tintColor = .clear textField.tintColor = .clear
textField.textColor = .clear
textField.delegate = self textField.delegate = self
textField.addTarget(self, action: #selector(smsCodeInputChanged), for: .editingChanged) textField.addTarget(self, action: #selector(smsCodeInputChanged), for: .editingChanged)
return textField return textField
...@@ -62,6 +62,7 @@ class YHSmsCodeInputView: UIView { ...@@ -62,6 +62,7 @@ class YHSmsCodeInputView: UIView {
textField.snp.makeConstraints { make in textField.snp.makeConstraints { make in
make.edges.equalTo(self) make.edges.equalTo(self)
} }
textField.becomeFirstResponder()
// 输入框数量 // 输入框数量
let count = config.count let count = config.count
...@@ -75,7 +76,10 @@ class YHSmsCodeInputView: UIView { ...@@ -75,7 +76,10 @@ class YHSmsCodeInputView: UIView {
let isEven = (config.count % 2 == 0) let isEven = (config.count % 2 == 0)
for index in 1...count { for index in 1...count {
let inputView = YNSmsCodeView(frame: CGRectZero) let inputView = YHSmsCodeView(frame: CGRectZero)
if (index == 1) {
inputView.showCursor = true
}
inputView.tag = codeViewBaseTag + index inputView.tag = codeViewBaseTag + index
self.addSubview(inputView) self.addSubview(inputView)
...@@ -118,19 +122,33 @@ class YHSmsCodeInputView: UIView { ...@@ -118,19 +122,33 @@ class YHSmsCodeInputView: UIView {
@objc func smsCodeInputChanged(input:UITextField) { @objc func smsCodeInputChanged(input:UITextField) {
if input.text == nil { return } guard let text = input.text else {
let codeView = self.viewWithTag(codeViewBaseTag+1) as? YHSmsCodeView
codeView?.showCursor = true
return
}
if (text.count > config.count) { return }
for index in 0..<config.count {
let codeView = self.viewWithTag(codeViewBaseTag+index+1) as! YHSmsCodeView
codeView.text = (index < text.count ? String(text[index]) : "")
codeView.showCursor = (index == text.count)
}
for (index, char) in input.text!.enumerated() { for (index, char) in input.text!.enumerated() {
if (index < config.count) { if (index < config.count) {
var codeView = self.viewWithTag(codeViewBaseTag+index+1) as? YNSmsCodeView let codeView = self.viewWithTag(codeViewBaseTag+index+1) as? YHSmsCodeView
codeView?.text = String(char) codeView?.text = String(char)
} }
} }
for index in (0 ..< config.count) { if (text.count == config.count) {
// if index < input.text?.count { textField.resignFirstResponder()
if let complete = self.complete {
// } complete(text)
}
} }
} }
} }
...@@ -139,6 +157,19 @@ extension YHSmsCodeInputView: UITextFieldDelegate { ...@@ -139,6 +157,19 @@ extension YHSmsCodeInputView: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return true guard let text = textField.text else {
return true
}
if (string.count <= 0) { // 删除字符操作
return true
}
if !string.isDigits { // 必须是数字
return false
}
// 限制字数
let textLength = text.charactersArray.count + string.charactersArray.count - range.length
return textLength <= config.count
} }
} }
...@@ -7,27 +7,17 @@ ...@@ -7,27 +7,17 @@
import UIKit import UIKit
class YNSmsCodeView: UIView { class YHSmsCodeView: UIView {
// 是否显示光标 // 是否显示光标
var showCursor : Bool = false { var showCursor : Bool = false {
didSet { didSet {
if (showCursor) { if showCursor {
UIView.animate(withDuration: 0.5, animations: { self.cursor.layer .removeAllAnimations()
[weak self] in self.animaitonShow(true)
self?.cursor.isHidden = false
}, completion: {
[weak self] finishd in
UIView.animate(withDuration: 0.5, animations: {
self?.cursor.isHidden = true
})
})
} else { } else {
cursor.layer.removeAllAnimations() self.cursor.layer .removeAllAnimations()
cursor.isHidden = true self.cursor.isHidden = true
} }
} }
} }
...@@ -47,10 +37,10 @@ class YNSmsCodeView: UIView { ...@@ -47,10 +37,10 @@ class YNSmsCodeView: UIView {
var cursor : UIView = { var cursor : UIView = {
let cursor = UIView() let cursor = UIView()
cursor.backgroundColor = .black cursor.backgroundColor = .black
cursor.alpha = 0 cursor.isHidden = true
return cursor return cursor
}() }()
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
super.init(coder: coder) super.init(coder: coder)
} }
...@@ -70,13 +60,24 @@ class YNSmsCodeView: UIView { ...@@ -70,13 +60,24 @@ class YNSmsCodeView: UIView {
self.addSubview(cursor) self.addSubview(cursor)
} }
@objc func animaitonShow(_ show: Bool) {
override func layoutSubviews() {
// let x = (self.frame.size.width - self.label.frame.size.width) / 2.0; if !self.showCursor { return }
// let y = (self.frame.size.height - self.label.frame.size.height) / 2.0;
self.label.frame = self.bounds
UIView.animate(withDuration: 0.8, animations: {
self.cursor.isHidden = !show
}, completion: { (finishd) in
if self.showCursor {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) {
self.animaitonShow(!show)
}
}
})
}
override func layoutSubviews() {
self.label.frame = self.bounds
self.cursor.bounds = CGRectMake(0, 0, 1.5, self.frame.size.height - 20); self.cursor.bounds = CGRectMake(0, 0, 1.5, self.frame.size.height - 20);
self.cursor.center = CGPoint(x:CGRectGetMidX(self.bounds) , y: CGRectGetMidY(self.bounds)) self.cursor.center = CGPoint(x:CGRectGetMidX(self.bounds) , y: CGRectGetMidY(self.bounds))
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment