Skip to content

Commit

Permalink
Change how errors work, clear error on resign, add new functionality …
Browse files Browse the repository at this point in the history
…to clear the view
  • Loading branch information
Fawxy committed Jul 26, 2018
1 parent eac440c commit cf6b6ae
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CBPinEntryView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'CBPinEntryView'
s.version = '1.5.3'
s.version = '1.5.4'
s.summary = 'A view for entering arbitrary length numerical pins or codes written in Swift 4.0.'

# This description is used to generate tags and improve search results.
Expand Down
24 changes: 17 additions & 7 deletions CBPinEntryView/Classes/CBPinEntryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public protocol CBPinEntryViewDelegate: class {
private var stackView: UIStackView?
private var textField: UITextField!

fileprivate var errorMode: Bool = false
open var errorMode: Bool = false

fileprivate var entryButtons: [UIButton] = [UIButton]()

Expand Down Expand Up @@ -195,19 +195,31 @@ public protocol CBPinEntryViewDelegate: class {
textField.becomeFirstResponder()
}

open func toggleError() {
if !errorMode {
open func setError(isError: Bool) {
if isError {
errorMode = true
for button in entryButtons {
button.layer.borderColor = entryErrorBorderColour.cgColor
button.layer.borderWidth = entryBorderWidth
}
} else {
errorMode = false
for button in entryButtons {
button.layer.borderColor = entryBorderColour.cgColor
}
}
}

open func clearEntry() {
setError(isError: false)
textField.text = ""
for button in entryButtons {
button.setTitle("", for: .normal)
}

errorMode = !errorMode
if let firstButton = entryButtons.first {
didPressCodeButton(firstButton)
}
}

open func getPinAsInt() -> Int? {
Expand Down Expand Up @@ -235,9 +247,7 @@ public protocol CBPinEntryViewDelegate: class {
@discardableResult open override func resignFirstResponder() -> Bool {
super.resignFirstResponder()

entryButtons.forEach {
$0.layer.borderColor = entryDefaultBorderColour.cgColor
}
setError(isError: false)

return textField.resignFirstResponder()
}
Expand Down
Binary file modified Example/.DS_Store
Binary file not shown.
28 changes: 23 additions & 5 deletions Example/CBPinEntryView/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<device id="retina4_0" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand Down Expand Up @@ -66,21 +66,39 @@
</constraints>
</stackView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="e7G-Qq-ufL">
<rect key="frame" x="141" y="252.5" width="38" height="30"/>
<state key="normal" title="Press"/>
<rect key="frame" x="136" y="252.5" width="49" height="30"/>
<state key="normal" title="Submit"/>
<connections>
<action selector="pressedButton:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="ohr-U9-pHP"/>
<action selector="pressedGetCode:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="uAS-Zl-ovd"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="qcc-XO-4s7">
<rect key="frame" x="118" y="302.5" width="84" height="30"/>
<state key="normal" title="Toggle error"/>
<connections>
<action selector="toggleError:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="CMR-iR-lYp"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Gkp-B4-Rf5">
<rect key="frame" x="142" y="352.5" width="36" height="30"/>
<state key="normal" title="Clear"/>
<connections>
<action selector="pressedClear:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="s4X-QJ-IXz"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="0.94509803920000002" green="0.96078431369999995" blue="0.97254901959999995" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="qcc-XO-4s7" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="Cj4-HF-I59"/>
<constraint firstItem="l00-Oi-KOV" firstAttribute="top" secondItem="8mn-8t-3Ic" secondAttribute="bottom" constant="30" id="Fto-dd-m9g"/>
<constraint firstItem="Gkp-B4-Rf5" firstAttribute="top" secondItem="qcc-XO-4s7" secondAttribute="bottom" constant="20" id="Tct-Nx-GvP"/>
<constraint firstItem="8mn-8t-3Ic" firstAttribute="centerY" secondItem="kh9-bI-dsS" secondAttribute="centerY" multiplier="0.5" id="e15-Jp-UgT"/>
<constraint firstItem="l00-Oi-KOV" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="g8I-HD-8da"/>
<constraint firstItem="e7G-Qq-ufL" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="hkh-Ok-IeY"/>
<constraint firstItem="8mn-8t-3Ic" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="kSo-Zi-uTG"/>
<constraint firstItem="Gkp-B4-Rf5" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="pKl-BL-O1c"/>
<constraint firstItem="e7G-Qq-ufL" firstAttribute="top" secondItem="l00-Oi-KOV" secondAttribute="bottom" constant="40" id="rQd-Pj-ZOf"/>
<constraint firstItem="qcc-XO-4s7" firstAttribute="top" secondItem="e7G-Qq-ufL" secondAttribute="bottom" constant="20" id="tyB-mB-qJl"/>
</constraints>
</view>
<connections>
Expand Down
15 changes: 14 additions & 1 deletion Example/CBPinEntryView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,22 @@ class ViewController: UIViewController {
}
@IBOutlet var stringOutputLabel: UILabel!

@IBAction func pressedButton(_ sender: UIButton) {
@IBAction func pressedGetCode(_ sender: UIButton) {
stringOutputLabel.text = pinEntryView.getPinAsString()
print(pinEntryView.getPinAsInt() ?? "Nothing entered")
pinEntryView.resignFirstResponder()
}

@IBAction func toggleError(_ sender: UIButton) {
if !pinEntryView.errorMode {
pinEntryView.setError(isError: true)
} else {
pinEntryView.setError(isError: false)
}
}

@IBAction func pressedClear(_ sender: UIButton) {
pinEntryView.clearEntry()
}
}

Expand Down

0 comments on commit cf6b6ae

Please sign in to comment.