前言
開發(fā)中很多地方都會(huì)遇到密碼輸入铺敌,這時(shí)候往往需要根據(jù)UI設(shè)計(jì)自定義踢涌。這里遵守UIKeyInput
黔龟,實(shí)現(xiàn)協(xié)議中的方法,讓自定義View可以進(jìn)行文字輸入宙帝;再通過func draw(_ rect: CGRect)
繪制現(xiàn)自定義UI丧凤;使用配置類來統(tǒng)一接口;使用代理來管理各種輸入相關(guān)的事件步脓。文章末尾有提供OC和Swift雙語(yǔ)的CLDemo下載,這里講解就使用Swift浩螺。
1.遵守UIKeyInput
協(xié)議靴患,實(shí)現(xiàn)文字輸入
遵守UIKeyInput
協(xié)議,實(shí)現(xiàn)協(xié)議中- (BOOL)hasText
要出、- (void)insertText:(NSString *)text
鸳君、- (void)deleteBackward
這三個(gè)方法。這里方便閱讀患蹂,單獨(dú)抽離成為一個(gè)extension
或颊。
extension CLPasswordInputView: UIKeyInput {
var hasText: Bool {
return text.length > 0
}
func insertText(_ text: String) {
if self.text.length < config.passwordNum {
let cs = NSCharacterSet.init(charactersIn: "0123456789").inverted
let string = text.components(separatedBy: cs).joined(separator: "")
let basicTest = text == string
if basicTest {
self.text.append(text)
delegate?.passwordInputViewDidChange(passwordInputView: self)
if self.text.length == config.passwordNum {
delegate?.passwordInputViewCompleteInput(passwordInputView: self)
}
setNeedsDisplay()
}
}
}
func deleteBackward() {
if text.length > 0 {
text.deleteCharacters(in: NSRange(location: text.length - 1, length: 1))
delegate?.passwordInputViewDidChange(passwordInputView: self)
}
delegate?.passwordInputViewDidDeleteBackward(passwordInputView: self)
setNeedsDisplay()
}
}
2.重寫override func draw(_ rect: CGRect)
砸紊,繪制自定義UI
根據(jù)配置信息,以及當(dāng)前文字輸入囱挑,繪制自定義UI醉顽,這里講繪制代碼和一些基本代碼寫在一起,單獨(dú)抽離成extension
平挑。
extension CLPasswordInputView {
override func becomeFirstResponder() -> Bool {
if !isShow {
delegate?.passwordInputViewBeginInput(passwordInputView: self)
}
isShow = true;
return super.becomeFirstResponder()
}
override func resignFirstResponder() -> Bool {
if isShow {
delegate?.passwordInputViewEndInput(passwordInputView: self)
}
isShow = false
return super.resignFirstResponder()
}
var keyboardType: UIKeyboardType {
get {
return .numberPad
}
set {
}
}
override var canBecomeFirstResponder: Bool {
return true
}
override var canResignFirstResponder: Bool {
return true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
if !isFirstResponder {
_ = becomeFirstResponder()
}
}
func updateWithConfig(config: ((CLPasswordInputViewConfigure) -> Void)?) -> Void {
config?(self.config)
backgroundColor = self.config.backgroundColor
setNeedsDisplay()
}
override func layoutSubviews() {
super.layoutSubviews()
setNeedsDisplay()
}
override func draw(_ rect: CGRect) {
let height = rect.size.height
let width = rect.size.width
let squareWidth = min(max(min(height, config.squareWidth), config.pointRadius * 4), height)
let pointRadius = min(config.pointRadius, squareWidth * 0.5) * 0.8
let middleSpace = CGFloat(width - CGFloat(config.passwordNum) * squareWidth) / CGFloat(CGFloat(config.passwordNum - 1) + config.spaceMultiple * 2)
let leftSpace = middleSpace * config.spaceMultiple
let y = (height - squareWidth) * 0.5
let context = UIGraphicsGetCurrentContext()
for i in 0 ..< config.passwordNum {
context?.addRect(CGRect(x: leftSpace + CGFloat(i) * squareWidth + CGFloat(i) * middleSpace, y: y, width: squareWidth, height: squareWidth))
context?.setLineWidth(1)
context?.setStrokeColor(config.rectColor.cgColor)
context?.setFillColor(config.rectBackgroundColor.cgColor)
}
context?.drawPath(using: .fillStroke)
context?.setFillColor(config.pointColor.cgColor)
for i in 0 ..< text.length {
context?.addArc(center: CGPoint(x: leftSpace + CGFloat(i + 1) * squareWidth + CGFloat(i) * middleSpace - squareWidth * 0.5, y: y + squareWidth * 0.5), radius: pointRadius, startAngle: 0, endAngle: .pi * 2, clockwise: true)
context?.drawPath(using: .fill)
}
}
}
3.使用配置類游添,來統(tǒng)一接口,生成基本配置信息
自定義UI過程中通熄,對(duì)于顏色唆涝,間隙,原點(diǎn)大小等唇辨,都需要留出接口廊酣,方便外部修改。一大堆屬性赏枚,對(duì)于使用者而言亡驰,并不友好,因?yàn)樗⒉恢滥男傩允潜仨毜奈撕兀男┦欠潜仨毜囊猓瑸榱俗屖褂谜叻奖闶褂茫@里單獨(dú)抽離出一個(gè)配置信息類诫睬,在內(nèi)部實(shí)現(xiàn)基礎(chǔ)配置煞茫,同時(shí)給出方法,讓外部可以修改某些屬性摄凡。
class CLPasswordInputViewConfigure: NSObject {
///密碼的位數(shù)
var passwordNum: UInt = 6
///邊框正方形的大小
var squareWidth: CGFloat = 50
///黑點(diǎn)的半徑
var pointRadius: CGFloat = 18 * 0.5
///邊距相對(duì)中間間隙倍數(shù)
var spaceMultiple: CGFloat = 5;
///黑點(diǎn)顏色
var pointColor: UIColor = UIColor.black
///邊框顏色
var rectColor: UIColor = UIColor.lightGray
///輸入框背景顏色
var rectBackgroundColor: UIColor = UIColor.white
///控件背景顏色
var backgroundColor: UIColor = UIColor.white
class func defaultConfig() -> CLPasswordInputViewConfigure {
let configure = CLPasswordInputViewConfigure()
return configure
}
}
外部修改配置的方法续徽,使用閉包,將基本配置回調(diào)到外部亲澡,同時(shí)在外部修改這些屬性后钦扭,對(duì)內(nèi)部UI進(jìn)行刷新,這里bloick是局部變量床绪,不會(huì)循環(huán)引用客情。
func updateWithConfig(config: ((CLPasswordInputViewConfigure) -> Void)?) -> Void {
config?(self.config)
backgroundColor = self.config.backgroundColor
setNeedsDisplay()
}
4.使用代理來管理各種輸入相關(guān)的事件
這里單獨(dú)創(chuàng)建一個(gè)協(xié)議,管理各種輸入事件癞己,同時(shí)通過extension
實(shí)現(xiàn)這些協(xié)議膀斋,這樣外部就可以選擇性的實(shí)現(xiàn)這些協(xié)議,而不是必須實(shí)現(xiàn)痹雅。
protocol CLPasswordInputViewDelegate: class {
///輸入改變
func passwordInputViewDidChange(passwordInputView:CLPasswordInputView) -> Void
///點(diǎn)擊刪除
func passwordInputViewDidDeleteBackward(passwordInputView:CLPasswordInputView) -> Void
///輸入完成
func passwordInputViewCompleteInput(passwordInputView:CLPasswordInputView) -> Void
///開始輸入
func passwordInputViewBeginInput(passwordInputView:CLPasswordInputView) -> Void
///結(jié)束輸入
func passwordInputViewEndInput(passwordInputView:CLPasswordInputView) -> Void
}
extension CLPasswordInputViewDelegate {
///輸入改變
func passwordInputViewDidChange(passwordInputView:CLPasswordInputView) -> Void {
}
///點(diǎn)擊刪除
func passwordInputViewDidDeleteBackward(passwordInputView:CLPasswordInputView) -> Void {
}
///輸入完成
func passwordInputViewCompleteInput(passwordInputView:CLPasswordInputView) -> Void {
}
///開始輸入
func passwordInputViewBeginInput(passwordInputView:CLPasswordInputView) -> Void {
}
///結(jié)束輸入
func passwordInputViewEndInput(passwordInputView:CLPasswordInputView) -> Void {
}
}
5.效果圖
這里簡(jiǎn)單錄制了一個(gè)效果仰担,更多請(qǐng)參考CLDemo
6.總結(jié)
為了方便大家學(xué)習(xí),這里提供了OC和Swift兩種語(yǔ)言分別實(shí)現(xiàn)的----->>>CLDemo绩社,如果對(duì)你有所幫助摔蓝,歡迎Star赂苗。