Skip to content

Commit c6f6778

Browse files
author
Athul Sai
committed
Added KWVerificationCodeViewDelegate to only enable the submit button when the code is valid
1 parent 263c789 commit c6f6778

6 files changed

Lines changed: 70 additions & 17 deletions

File tree

Example/KWVerificationCodeView/AppDelegate.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1313

1414
var window: UIWindow?
1515

16-
1716
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
1817
return true
1918
}
@@ -32,7 +31,5 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3231

3332
func applicationWillTerminate(_ application: UIApplication) {
3433
}
35-
36-
3734
}
3835

Example/KWVerificationCodeView/Base.lproj/Main.storyboard

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,74 @@
2323
<subviews>
2424
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ekS-Ew-GpT" userLabel="verificationCodeView" customClass="KWVerificationCodeView" customModule="KWVerificationCodeView">
2525
<rect key="frame" x="33" y="273" width="310" height="62"/>
26+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
2627
<constraints>
2728
<constraint firstAttribute="width" constant="310" id="SmR-e4-qbu"/>
2829
<constraint firstAttribute="height" constant="62" id="mda-A6-NvC"/>
2930
</constraints>
31+
<userDefinedRuntimeAttributes>
32+
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
33+
<integer key="value" value="5"/>
34+
</userDefinedRuntimeAttribute>
35+
<userDefinedRuntimeAttribute type="color" keyPath="underlineSelectedColor">
36+
<color key="value" red="0.068236636075229451" green="0.44572890228426398" blue="0.12406174271236946" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
37+
</userDefinedRuntimeAttribute>
38+
<userDefinedRuntimeAttribute type="color" keyPath="underlineColor">
39+
<color key="value" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
40+
</userDefinedRuntimeAttribute>
41+
</userDefinedRuntimeAttributes>
3042
</view>
31-
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Skh-28-8br" userLabel="submitButton">
32-
<rect key="frame" x="162" y="357" width="51" height="40"/>
43+
<button opaque="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Skh-28-8br" userLabel="submitButton">
44+
<rect key="frame" x="142.5" y="357" width="90" height="40"/>
45+
<color key="backgroundColor" red="1" green="0.5" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
3346
<constraints>
47+
<constraint firstAttribute="width" constant="90" id="AXu-JL-d2L"/>
3448
<constraint firstAttribute="height" constant="40" id="LpI-mz-Z5g"/>
3549
</constraints>
3650
<fontDescription key="fontDescription" type="system" weight="semibold" pointSize="15"/>
37-
<state key="normal" title="Submit"/>
51+
<state key="normal" title="Submit">
52+
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
53+
</state>
54+
<state key="disabled">
55+
<color key="titleColor" red="1" green="0.79129829853778799" blue="0.59988017074977373" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
56+
</state>
57+
<userDefinedRuntimeAttributes>
58+
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
59+
<integer key="value" value="5"/>
60+
</userDefinedRuntimeAttribute>
61+
</userDefinedRuntimeAttributes>
3862
<connections>
3963
<action selector="submitButtonTapped:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="NY6-ss-5Qf"/>
4064
</connections>
4165
</button>
4266
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="KWVerificationCode Example" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="6e0-Am-MUv" userLabel="titleLabel">
43-
<rect key="frame" x="16" y="185" width="343" height="24"/>
67+
<rect key="frame" x="16" y="159" width="343" height="24"/>
4468
<fontDescription key="fontDescription" style="UICTFontTextStyleTitle3"/>
45-
<nil key="textColor"/>
69+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
4670
<nil key="highlightedColor"/>
4771
</label>
4872
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Enter Code" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="P6k-pN-VEP" userLabel="promptLabel">
49-
<rect key="frame" x="148" y="244" width="79" height="21"/>
50-
<fontDescription key="fontDescription" type="system" weight="medium" pointSize="15"/>
73+
<rect key="frame" x="149" y="233" width="77" height="18"/>
74+
<fontDescription key="fontDescription" type="system" pointSize="15"/>
5175
<nil key="textColor"/>
5276
<nil key="highlightedColor"/>
5377
</label>
5478
</subviews>
55-
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
79+
<color key="backgroundColor" red="0.93725490570000003" green="0.93725490570000003" blue="0.95686274770000002" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
5680
<constraints>
57-
<constraint firstItem="ekS-Ew-GpT" firstAttribute="top" secondItem="P6k-pN-VEP" secondAttribute="bottom" constant="8" id="H7u-LC-n82"/>
58-
<constraint firstItem="ekS-Ew-GpT" firstAttribute="top" secondItem="6e0-Am-MUv" secondAttribute="bottom" constant="64" id="HQQ-Ne-ux5"/>
81+
<constraint firstItem="ekS-Ew-GpT" firstAttribute="top" secondItem="P6k-pN-VEP" secondAttribute="bottom" constant="22" id="H7u-LC-n82"/>
82+
<constraint firstItem="ekS-Ew-GpT" firstAttribute="top" secondItem="6e0-Am-MUv" secondAttribute="bottom" constant="90" id="HQQ-Ne-ux5"/>
5983
<constraint firstItem="6e0-Am-MUv" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="16" id="K9Y-GQ-xFr"/>
6084
<constraint firstAttribute="trailing" secondItem="6e0-Am-MUv" secondAttribute="trailing" constant="16" id="NnO-Pl-xaO"/>
6185
<constraint firstItem="Skh-28-8br" firstAttribute="top" secondItem="ekS-Ew-GpT" secondAttribute="bottom" constant="22" id="UAT-si-Rcy"/>
6286
<constraint firstItem="Skh-28-8br" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="bUo-66-X5C"/>
63-
<constraint firstItem="P6k-pN-VEP" firstAttribute="top" secondItem="6e0-Am-MUv" secondAttribute="bottom" constant="35" id="gvO-6d-BKX"/>
6487
<constraint firstItem="P6k-pN-VEP" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="nb6-py-4Yg"/>
6588
<constraint firstItem="ekS-Ew-GpT" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="rI6-Fp-Ztx"/>
6689
<constraint firstItem="ekS-Ew-GpT" firstAttribute="centerY" secondItem="kh9-bI-dsS" secondAttribute="centerY" constant="-30" id="vWu-wt-SXh"/>
6790
</constraints>
6891
</view>
6992
<connections>
93+
<outlet property="submitButton" destination="Skh-28-8br" id="c7l-qQ-HLf"/>
7094
<outlet property="verificationCodeView" destination="ekS-Ew-GpT" id="AtV-hT-QUf"/>
7195
</connections>
7296
</viewController>

Example/KWVerificationCodeView/Images.xcassets/AppIcon.appiconset/Contents.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
{
22
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"size" : "20x20",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"size" : "20x20",
11+
"scale" : "3x"
12+
},
313
{
414
"idiom" : "iphone",
515
"size" : "29x29",
@@ -35,4 +45,4 @@
3545
"version" : 1,
3646
"author" : "xcode"
3747
}
38-
}
48+
}

Example/KWVerificationCodeView/VerificationCodeViewController.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,29 @@ class VerificationCodeViewController: UIViewController {
1313

1414
// MARK: - IBOutlets
1515
@IBOutlet weak var verificationCodeView: KWVerificationCodeView!
16+
@IBOutlet weak var submitButton: UIButton!
1617

1718
// MARK: - Lifecycle
1819
override func viewDidLoad() {
1920
super.viewDidLoad()
21+
submitButton.isEnabled = false
22+
verificationCodeView.delegate = self
2023
}
21-
24+
2225
// MARK: - IBAction
2326
@IBAction func submitButtonTapped(_ sender: UIButton) {
2427
if verificationCodeView.hasValidCode() {
2528
let alertController = UIAlertController(title: "Success", message: "Code is \(verificationCodeView.getVerificationCode())", preferredStyle: .alert)
2629
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
2730
alertController.addAction(okAction)
28-
self.present(alertController, animated: true, completion: nil)
31+
present(alertController, animated: true, completion: nil)
2932
}
3033
}
3134
}
35+
36+
// MARK: - KWVerificationCodeViewDelegate
37+
extension VerificationCodeViewController: KWVerificationCodeViewDelegate {
38+
func didChangeVerificationCode() {
39+
self.submitButton.isEnabled = verificationCodeView.hasValidCode()
40+
}
41+
}

KWVerificationCodeView/Classes/KWTextFieldView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import UIKit
1111
protocol KWTextFieldDelegate: class {
1212
func moveToNext(_ textFieldView: KWTextFieldView)
1313
func moveToPrevious(_ textFieldView: KWTextFieldView, oldCode: String)
14+
func didEndEditing()
1415
}
1516

1617
@IBDesignable class KWTextFieldView: UIView {
@@ -95,6 +96,7 @@ extension KWTextFieldView: UITextFieldDelegate {
9596
numberTextField.text = " "
9697
}
9798

99+
delegate?.didEndEditing()
98100
updateUnderline()
99101

100102
return newString.characters.count <= type(of: self).maxCharactersLength

KWVerificationCodeView/Classes/KWVerificationCodeView.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
import UIKit
1010

11+
public protocol KWVerificationCodeViewDelegate: class {
12+
func didChangeVerificationCode()
13+
}
14+
1115
@IBDesignable open class KWVerificationCodeView: UIView {
1216

1317
// MARK: - Constants
@@ -42,6 +46,8 @@ import UIKit
4246
return [self.textFieldView1, self.textFieldView2, self.textFieldView3, self.textFieldView4]
4347
}()
4448

49+
weak public var delegate: KWVerificationCodeViewDelegate?
50+
4551
// MARK: - Lifecycle
4652
override init(frame: CGRect) {
4753
super.init(frame: frame)
@@ -102,4 +108,8 @@ extension KWVerificationCodeView: KWTextFieldDelegate {
102108
textFieldViews[validIndex].activate()
103109
textFieldViews[validIndex].reset()
104110
}
111+
112+
func didEndEditing() {
113+
delegate?.didChangeVerificationCode()
114+
}
105115
}

0 commit comments

Comments
 (0)