├── .DS_Store
├── SwiftyVerificationCodeView
├── SwiftyVerificationCodeView
│ ├── VerificationCode.gif
│ ├── SwiftyTextField.swift
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── ViewController.swift
│ ├── Base.lproj
│ │ ├── Main.storyboard
│ │ └── LaunchScreen.storyboard
│ ├── AppDelegate.swift
│ └── SwiftyVerificationCodeView.swift
└── SwiftyVerificationCodeView.xcodeproj
│ ├── project.xcworkspace
│ └── contents.xcworkspacedata
│ └── project.pbxproj
├── README.md
├── LICENSE
└── .gitignore
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KFCFans/SwiftyVerificationCodeView/HEAD/.DS_Store
--------------------------------------------------------------------------------
/SwiftyVerificationCodeView/SwiftyVerificationCodeView/VerificationCode.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KFCFans/SwiftyVerificationCodeView/HEAD/SwiftyVerificationCodeView/SwiftyVerificationCodeView/VerificationCode.gif
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SwiftyVerificationCodeView
2 | 仿滴滴短信验证码输入框 ,swift3实现
3 |
4 | 
5 |
--------------------------------------------------------------------------------
/SwiftyVerificationCodeView/SwiftyVerificationCodeView.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/SwiftyVerificationCodeView/SwiftyVerificationCodeView/SwiftyTextField.swift:
--------------------------------------------------------------------------------
1 | //
2 | // TestTextField.swift
3 | // EULoginTemp
4 | //
5 | // Created by lip on 17/4/6.
6 | // Copyright © 2017年 lip. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 |
12 | protocol SwiftyTextFieldDeleteDelegate {
13 | func didClickBackWard()
14 | }
15 |
16 | class SwiftyTextField: UITextField {
17 |
18 | var deleteDelegate:SwiftyTextFieldDeleteDelegate?
19 |
20 | override func deleteBackward() {
21 | super.deleteBackward()
22 | deleteDelegate?.didClickBackWard()
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/SwiftyVerificationCodeView/SwiftyVerificationCodeView/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "29x29",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "29x29",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "40x40",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "40x40",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "60x60",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "60x60",
31 | "scale" : "3x"
32 | }
33 | ],
34 | "info" : {
35 | "version" : 1,
36 | "author" : "xcode"
37 | }
38 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 LoveAlwaysYoung
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/SwiftyVerificationCodeView/SwiftyVerificationCodeView/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/SwiftyVerificationCodeView/SwiftyVerificationCodeView/ViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.swift
3 | // SwiftyVerificationCodeView
4 | //
5 | // Created by lip on 17/4/6.
6 | // Copyright © 2017年 lip. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | class ViewController: UIViewController,SwiftyVerificationCodeViewDelegate {
12 |
13 | var showlabel = UILabel()
14 |
15 | override func viewDidLoad() {
16 | super.viewDidLoad()
17 | let v = SwiftyVerificationCodeView(frame: CGRect(x: 0, y: 100, width: UIScreen.main.bounds.width, height: 100))
18 | v.delegate = self
19 | view.backgroundColor = UIColor.white
20 | view.addSubview(v)
21 |
22 | showlabel.frame = CGRect(x: 0, y: 300, width: UIScreen.main.bounds.width, height: 30)
23 | showlabel.textAlignment = .center
24 | showlabel.font = UIFont.boldSystemFont(ofSize: 30)
25 | view.addSubview(showlabel)
26 | }
27 |
28 | override func didReceiveMemoryWarning() {
29 | super.didReceiveMemoryWarning()
30 | // Dispose of any resources that can be recreated.
31 | }
32 |
33 | func verificationCodeDidFinishedInput(verificationCodeView: SwiftyVerificationCodeView, code: String) {
34 | // 登陆逻辑
35 | showlabel.text = code
36 |
37 | if code != "4396" {
38 | verificationCodeView.cleanVerificationCodeView()
39 | }
40 | }
41 |
42 |
43 | }
44 |
45 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Xcode
2 | #
3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4 |
5 | ## Build generated
6 | build/
7 | DerivedData/
8 |
9 | ## Various settings
10 | *.pbxuser
11 | !default.pbxuser
12 | *.mode1v3
13 | !default.mode1v3
14 | *.mode2v3
15 | !default.mode2v3
16 | *.perspectivev3
17 | !default.perspectivev3
18 | xcuserdata/
19 |
20 | ## Other
21 | *.moved-aside
22 | *.xcuserstate
23 |
24 | ## Obj-C/Swift specific
25 | *.hmap
26 | *.ipa
27 | *.dSYM.zip
28 | *.dSYM
29 |
30 | ## Playgrounds
31 | timeline.xctimeline
32 | playground.xcworkspace
33 |
34 | # Swift Package Manager
35 | #
36 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
37 | # Packages/
38 | .build/
39 |
40 | # CocoaPods
41 | #
42 | # We recommend against adding the Pods directory to your .gitignore. However
43 | # you should judge for yourself, the pros and cons are mentioned at:
44 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
45 | #
46 | # Pods/
47 |
48 | # Carthage
49 | #
50 | # Add this line if you want to avoid checking in source code from Carthage dependencies.
51 | # Carthage/Checkouts
52 |
53 | Carthage/Build
54 |
55 | # fastlane
56 | #
57 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
58 | # screenshots whenever they are needed.
59 | # For more information about the recommended setup visit:
60 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md
61 |
62 | fastlane/report.xml
63 | fastlane/Preview.html
64 | fastlane/screenshots
65 | fastlane/test_output
66 |
--------------------------------------------------------------------------------
/SwiftyVerificationCodeView/SwiftyVerificationCodeView/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/SwiftyVerificationCodeView/SwiftyVerificationCodeView/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/SwiftyVerificationCodeView/SwiftyVerificationCodeView/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // SwiftyVerificationCodeView
4 | //
5 | // Created by lip on 17/4/6.
6 | // Copyright © 2017年 lip. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | @UIApplicationMain
12 | class AppDelegate: UIResponder, UIApplicationDelegate {
13 |
14 | var window: UIWindow?
15 |
16 |
17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
18 | window = UIWindow()
19 | window?.rootViewController = ViewController()
20 | window?.makeKeyAndVisible()
21 | return true
22 | }
23 |
24 | func applicationWillResignActive(_ application: UIApplication) {
25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
27 | }
28 |
29 | func applicationDidEnterBackground(_ application: UIApplication) {
30 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
31 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
32 | }
33 |
34 | func applicationWillEnterForeground(_ application: UIApplication) {
35 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
36 | }
37 |
38 | func applicationDidBecomeActive(_ application: UIApplication) {
39 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
40 | }
41 |
42 | func applicationWillTerminate(_ application: UIApplication) {
43 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
44 | }
45 |
46 |
47 | }
48 |
49 |
--------------------------------------------------------------------------------
/SwiftyVerificationCodeView/SwiftyVerificationCodeView/SwiftyVerificationCodeView.swift:
--------------------------------------------------------------------------------
1 | //
2 | // SwiftyVerificationCodeView.swift
3 | // EULoginTemp
4 | //
5 | // Created by lip on 17/4/6.
6 | // Copyright © 2017年 lip. All rights reserved.
7 | //
8 |
9 | import UIKit
10 | /**
11 | 滴滴验证码的逻辑
12 | 不允许用户点击,顺序已经定死,可以用删除键回退,最后一个输完自动登陆
13 | */
14 |
15 | protocol SwiftyVerificationCodeViewDelegate {
16 | func verificationCodeDidFinishedInput(verificationCodeView:SwiftyVerificationCodeView,code:String)
17 | }
18 | class SwiftyVerificationCodeView: UIView {
19 |
20 | /// 代理回调
21 | var delegate:SwiftyVerificationCodeViewDelegate?
22 |
23 | /// 一堆框框的数组
24 | var textfieldarray = [UITextField]()
25 |
26 | /// 框框之间的间隔
27 | let margin:CGFloat = 10
28 |
29 | /// 框框的大小
30 | let width:CGFloat = 50
31 |
32 | /// 框框个数
33 | var numOfRect = 4
34 |
35 | /// 构造函数
36 | ///
37 | /// - Parameters:
38 | /// - frame: frame,宽度最好设置为屏幕宽度
39 | /// - num: 框框个数,默认 4 个
40 | /// - margin: 框框之间的间距,默认 10
41 | init(frame: CGRect,num:Int = 4,margin:CGFloat = 10) {
42 | super.init(frame: frame)
43 | setupUI()
44 | }
45 |
46 |
47 | required init?(coder aDecoder: NSCoder) {
48 | fatalError("init(coder:) has not been implemented")
49 | }
50 |
51 | func cleanVerificationCodeView(){
52 |
53 | for tv in textfieldarray {
54 | tv.text = ""
55 | }
56 | textfieldarray.first?.becomeFirstResponder()
57 |
58 |
59 | }
60 |
61 | }
62 |
63 | // MARK: - UI 相关方法
64 | extension SwiftyVerificationCodeView{
65 |
66 | fileprivate func setupUI(){
67 |
68 | // 不允许用户直接操作验证码框
69 | self.isUserInteractionEnabled = false
70 |
71 | // 计算左间距
72 | let leftmargin = (UIScreen.main.bounds.width - width * CGFloat(numOfRect) - CGFloat(numOfRect - 1) * margin) / 2
73 |
74 | // 创建 n个 UITextFiedl
75 | for i in 0..UITextField{
94 |
95 | let tv = SwiftyTextField(frame: frame)
96 | tv.borderStyle = .line
97 | tv.textAlignment = .center
98 | tv.font = UIFont.boldSystemFont(ofSize: 40)
99 | tv.textColor = UIColor.init(red: 51/255, green: 51/255, blue: 51/255, alpha: 1)
100 | tv.delegate = self
101 | tv.deleteDelegate = self
102 | addSubview(tv)
103 | tv.keyboardType = .numberPad
104 | return tv
105 |
106 | }
107 |
108 | }
109 |
110 |
111 |
112 | extension SwiftyVerificationCodeView:UITextFieldDelegate,SwiftyTextFieldDeleteDelegate{
113 |
114 | func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
115 |
116 | if !textField.hasText {
117 |
118 | // tag 对应数组下标
119 | let index = textField.tag
120 |
121 | textField.resignFirstResponder()
122 | if index == numOfRect - 1 {
123 | textfieldarray[index].text = string
124 | // 拼接结果
125 | var code = ""
126 | for tv in textfieldarray{
127 | code += tv.text ?? ""
128 | }
129 | delegate?.verificationCodeDidFinishedInput(verificationCodeView: self, code: code)
130 | return false
131 | }
132 |
133 | textfieldarray[index].text = string
134 | textfieldarray[index + 1].becomeFirstResponder()
135 |
136 | }
137 | return false
138 |
139 | }
140 |
141 | /// 监听键盘删除键
142 | func didClickBackWard() {
143 |
144 | for i in 1..