最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS使用UIKeyInput自定義密碼輸入框的方法示例

 更新時間:2019年02月14日 10:11:59   作者:季末微夏  
這篇文章主要給大家介紹了關(guān)于iOS如何使用UIKeyInput自定義密碼輸入框的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

前言

開發(fā)中很多地方都會遇到密碼輸入,這時候往往需要根據(jù)UI設(shè)計自定義。這里遵守UIKeyInput,實現(xiàn)協(xié)議中的方法,讓自定義View可以進行文字輸入;再通過func draw(_ rect: CGRect)繪制現(xiàn)自定義UI;使用配置類來統(tǒng)一接口;使用代理來管理各種輸入相關(guān)的事件。文章末尾有提供OC和Swift雙語的CLDemo下載,這里講解就使用Swift。

1.遵守UIKeyInput協(xié)議,實現(xiàn)文字輸入

遵守UIKeyInput協(xié)議,實現(xiàn)協(xié)議中- (BOOL)hasText - (void)insertText:(NSString *)text、 - (void)deleteBackward這三個方法。

這里方便閱讀,單獨抽離成為一個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ù)配置信息,以及當前文字輸入,繪制自定義UI,這里講繪制代碼和一些基本代碼寫在一起,單獨抽離成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過程中,對于顏色,間隙,原點大小等,都需要留出接口,方便外部修改。一大堆屬性,對于使用者而言,并不友好,因為他并不知道哪些屬性是必須的,哪些是非必須的,為了讓使用者方便使用,這里單獨抽離出一個配置信息類,在內(nèi)部實現(xiàn)基礎(chǔ)配置,同時給出方法,讓外部可以修改某些屬性。

class CLPasswordInputViewConfigure: NSObject {
 ///密碼的位數(shù)
 var passwordNum: UInt = 6
 ///邊框正方形的大小
 var squareWidth: CGFloat = 50
 ///黑點的半徑
 var pointRadius: CGFloat = 18 * 0.5
 ///邊距相對中間間隙倍數(shù)
 var spaceMultiple: CGFloat = 5;
 ///黑點顏色
 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)到外部,同時在外部修改這些屬性后,對內(nèi)部UI進行刷新。

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

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

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

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

5.效果圖

這里簡單錄制了一個效果,更多請參考CLDemo (本地下載

效果圖.gif

6.總結(jié)

為了方便大家學習,這里提供了OC和Swift兩種語言分別實現(xiàn)的----->>>CLDemo (本地下載),如果對你有所幫助,歡迎Star。

好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • iOS購物分類模塊的實現(xiàn)方案

    iOS購物分類模塊的實現(xiàn)方案

    這篇文章主要為大家詳細介紹了iOS購物分類模塊的實現(xiàn)方案,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • iOS應(yīng)用運用設(shè)計模式中的Strategy策略模式的開發(fā)實例

    iOS應(yīng)用運用設(shè)計模式中的Strategy策略模式的開發(fā)實例

    這篇文章主要介紹了iOS應(yīng)用開發(fā)中對設(shè)計模式中的Strategy策略模式的運用,例子采用傳統(tǒng)的Objective-C語言代碼演示,需要的朋友可以參考下
    2016-03-03
  • iOS 高德地圖仿微信發(fā)送實時位置

    iOS 高德地圖仿微信發(fā)送實時位置

    這篇文章主要介紹了iOS 高德地圖仿微信發(fā)送實時位置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Xcode中iOS應(yīng)用開發(fā)的一般項目目錄結(jié)構(gòu)和流程簡介

    Xcode中iOS應(yīng)用開發(fā)的一般項目目錄結(jié)構(gòu)和流程簡介

    這篇文章主要介紹了Xcode中iOS應(yīng)用開發(fā)的一般項目目錄結(jié)構(gòu)和流程簡介,包括項目所需的一些平臺路徑如模擬器路徑等的介紹,需要的朋友可以參考下
    2016-02-02
  • touchesBegan: withEvent: 不執(zhí)行解決

    touchesBegan: withEvent: 不執(zhí)行解決

    這篇文章主要介紹了touchesBegan: withEvent: 不執(zhí)行解決的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • IOS本地日志記錄解決方案

    IOS本地日志記錄解決方案

    我們在項目中日志記錄這塊也算是比較重要的,有時候用戶程序出什么問題,光靠服務(wù)器的日志還不能準確的找到問題。本文詳細介紹了IOS本地日志記錄解決方案。下面跟著小編一起來看下吧
    2017-03-03
  • iOS實現(xiàn)UIButton的拖拽功能

    iOS實現(xiàn)UIButton的拖拽功能

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)UIButton的拖拽功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • iOS為UIView設(shè)置陰影效果

    iOS為UIView設(shè)置陰影效果

    現(xiàn)在很多的開發(fā)者們都會在開發(fā)的時候加陰影效果,所以這篇文章跟大家分享下iOS為UIView設(shè)置陰影效果的實現(xiàn)過程,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-09-09
  • iOS11 下載之斷點續(xù)傳的bug的解決方法

    iOS11 下載之斷點續(xù)傳的bug的解決方法

    本篇文章主要介紹了iOS11 下載之斷點續(xù)傳的bug的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • ios xcode警告與錯誤的分析總結(jié)

    ios xcode警告與錯誤的分析總結(jié)

    這篇文章主要給大家介紹了關(guān)于ios xcode警告與錯誤的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-04-04

最新評論

安庆市| 黑河市| 东乡族自治县| 定陶县| 宁明县| 古蔺县| 合山市| 呼和浩特市| 池州市| 曲水县| 鹤山市| 鲁山县| 洛川县| 鄢陵县| 金川县| 牙克石市| 方山县| 密云县| 修水县| 华宁县| 靖安县| 博白县| 通榆县| 泗水县| 高阳县| 万山特区| 马公市| 湘阴县| 南涧| 蓝山县| 清原| 崇左市| 小金县| 六枝特区| 甘肃省| 拉孜县| 江山市| 恩平市| 正阳县| 延吉市| 邵阳市|