欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

iOS中怎么利用UIKeyInput自定義密碼輸入框

這篇文章將為大家詳細(xì)講解有關(guān)iOS中怎么利用UIKeyInput自定義密碼輸入框,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

成都創(chuàng)新互聯(lián)公司長(zhǎng)期為上千家客戶(hù)提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為新榮企業(yè)提供專(zhuān)業(yè)的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,新榮網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。

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.重寫(xiě)override func draw(_ rect: CGRect),繪制自定義UI

根據(jù)配置信息,以及當(dāng)前文字輸入,繪制自定義UI,這里講繪制代碼和一些基本代碼寫(xiě)在一起,單獨(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.使用配置類(lèi),來(lái)統(tǒng)一接口,生成基本配置信息

自定義UI過(guò)程中,對(duì)于顏色,間隙,原點(diǎn)大小等,都需要留出接口,方便外部修改。一大堆屬性,對(duì)于使用者而言,并不友好,因?yàn)樗⒉恢滥男傩允潜仨毜?,哪些是非必須的,為了讓使用者方便使用,這里單獨(dú)抽離出一個(gè)配置信息類(lèi),在內(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)行刷新。

func updateWithConfig(config: ((CLPasswordInputViewConfigure) -> Void)?) -> Void {  config?(self.config)  backgroundColor = self.config.backgroundColor  setNeedsDisplay() }

4.使用代理來(lái)管理各種輸入相關(guān)的事件

這里單獨(dú)創(chuàng)建一個(gè)協(xié)議,管理各種輸入事件,同時(shí)通過(guò)extension實(shí)現(xiàn)這些協(xié)議,這樣外部就可以選擇性的實(shí)現(xiàn)這些協(xié)議,而不是必須實(shí)現(xiàn)。

protocol CLPasswordInputViewDelegate { ///輸入改變 func passwordInputViewDidChange(passwordInputView:CLPasswordInputView) -> Void ///點(diǎn)擊刪除 func passwordInputViewDidDeleteBackward(passwordInputView:CLPasswordInputView) -> Void ///輸入完成 func passwordInputViewCompleteInput(passwordInputView:CLPasswordInputView) -> Void ///開(kāi)始輸入 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 { } ///開(kāi)始輸入 func passwordInputViewBeginInput(passwordInputView:CLPasswordInputView) -> Void { } ///結(jié)束輸入 func passwordInputViewEndInput(passwordInputView:CLPasswordInputView) -> Void { }}

關(guān)于iOS中怎么利用UIKeyInput自定義密碼輸入框就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

文章題目:iOS中怎么利用UIKeyInput自定義密碼輸入框
本文鏈接:http://aaarwkj.com/article34/jeejpe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、全網(wǎng)營(yíng)銷(xiāo)推廣、云服務(wù)器、外貿(mào)建站、建站公司、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
高潮少妇高潮少妇av| 欧美亚洲另类日韩综合网| 日韩暴露一区二区三区| 性生活视性生活大片日本| 青草成人在线视频观看| 不卡免费av在线高清| 欧美日韩国产精品高清| 亚洲一本一道久久香蕉| 成人黄片在线免费播放| 国内一级黄色片免费观看| av成熟一区二区三区| 久久久久久成人亚洲| 亚洲国际精品女人乱码| 国产精品久久久久精品日日三级| 日本午夜福利免费在线播放| 精品裸足人妻少妇二区三区| 国产九色91中文在线视频| 久久亚洲一区二区内射| 中文字幕乱码亚洲中文在线| 午夜91激情福利视频| 欧美黄色日本一区二区| 午夜夫妻生活视频在线观看 | 巨乳中文乱码国产一区二区| 亚洲少妇精品视频在线| 久久精品国产亚洲av制服| 日本不卡不二三区在线看| 亚洲视频一直看一直爽| 熟女一区二区三区免费视频| 欧美一区二区三区爽| 亚洲综合激情另类专区| 精品国产乱码一区二区三区四区| 打开网址国语一级黄色片| 国产午夜福利不卡在线观看| 午夜福利中文字幕在线亚洲| 国产精品一级片一区二区| 中文国产人精品久久蜜桃| 日产中文乱码字幕无线观看| 日韩精品一区二区三区人妻视频| 亚洲日本在线观看一区| 亚洲中文字幕少妇视频| 99精品午夜福利在线|