Skip to content

Commit

Permalink
- Added 'becomeFirstResponder' feature
Browse files Browse the repository at this point in the history
- Fixed issues with sizing the pinFields
- This fixes #8 and closes #9
  • Loading branch information
xornorik committed Aug 3, 2018
1 parent a3c29bd commit 36e1d88
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 35 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ pinView.font = UIFont.systemFont(ofSize: 15)
pinView.keyboardType = .phonePad
pinView.pinIinputAccessoryView = UIView()
pinView.placeholder = "******"
pinView.becomeFirstResponderAtIndex = 0
```
The `becomeFirstResponderAtIndex` property sets the pinField at the specified index as the first responder.

#### Styles
```swift
enum SVPinViewStyle : Int {
Expand Down
2 changes: 1 addition & 1 deletion SVPinView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Pod::Spec.new do |s|

s.name = "SVPinView"
s.version = "1.0.3"
s.version = "1.0.4"
s.summary = "SVPinView is a customisable library used for accepting alphanumeric pins or one-time passwords."

s.homepage = "https://github.com/xornorik/SVPinView"
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,4 @@
<Bucket
type = "0"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "../Source/Classes/SVPinView.swift"
timestampString = "551764455.36622"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "223"
endingLineNumber = "223"
landmarkName = "collectionView(_:cellForItemAt:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
5 changes: 3 additions & 2 deletions SVPinView/Example/SVPinViewExample/PinViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PinViewController: UIViewController {
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

//Setup background gradient
// Setup background gradient
let valenciaColor = UIColor(red: 218/255, green: 68/255, blue: 83/255, alpha: 1)
let discoColor = UIColor(red: 137/255, green: 33/255, blue: 107/255, alpha: 1)
setGradientBackground(view: self.view, colorTop: valenciaColor, colorBottom: discoColor)
Expand All @@ -40,6 +40,7 @@ class PinViewController: UIViewController {
pinView.fieldBackgroundColor = UIColor.white.withAlphaComponent(0.3)
pinView.fieldCornerRadius = 15
pinView.placeholder = "******"
pinView.becomeFirstResponderAtIndex = 0

pinView.font = UIFont.systemFont(ofSize: 15)
pinView.keyboardType = .phonePad
Expand Down Expand Up @@ -111,7 +112,7 @@ class PinViewController: UIViewController {
showAlert(title: "Success", message: "The Pin entered is \(pin)")
}

//MARK: Helper Functions
// MARK: Helper Functions
func showAlert(title:String, message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
Expand Down
43 changes: 29 additions & 14 deletions SVPinView/Source/Classes/SVPinView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class SVPinView: UIView {
public var font:UIFont = UIFont.systemFont(ofSize: 15)
public var keyboardType:UIKeyboardType = UIKeyboardType.phonePad
public var pinIinputAccessoryView:UIView = UIView()
public var becomeFirstResponderAtIndex:Int? = nil

fileprivate var password = [String]()
public var didFinishCallback: ((String)->())?
Expand All @@ -58,7 +59,7 @@ public class SVPinView: UIView {
let nib = UINib(nibName: "SVPinView", bundle: podBundle)
view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

//for CollectionView
// for CollectionView
let collectionViewNib = UINib(nibName: "SVPinCell", bundle:podBundle)
collectionView.register(collectionViewNib, forCellWithReuseIdentifier: reuseIdentifier)
flowLayout.scrollDirection = .vertical //weird!!!
Expand All @@ -74,7 +75,7 @@ public class SVPinView: UIView {
let index = nextTag - 100
let placeholderLabel = textField.superview?.viewWithTag(400) as! UILabel

//ensure single character in text box and trim spaces
// ensure single character in text box and trim spaces
if textField.text!.count > 1 {
textField.text?.removeFirst()
textField.text = { () -> String in
Expand All @@ -92,7 +93,7 @@ public class SVPinView: UIView {
}
}

//check if entered text is backspace
// check if entered text is backspace
if isBackSpace() {
nextTag = textField.tag - 1
} else {
Expand All @@ -109,10 +110,10 @@ public class SVPinView: UIView {
textField.resignFirstResponder()
}

//activate the placeholder if textField empty
// activate the placeholder if textField empty
placeholderLabel.isHidden = !textField.text!.isEmpty

//secure text after a bit
// secure text after a bit
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500), execute: {
if textField.text == "" {
textField.text = " "
Expand All @@ -123,11 +124,11 @@ public class SVPinView: UIView {
}
})

//store text
// store text
let text = textField.text ?? ""
let passwordIndex = index - 1
if password.count > (passwordIndex) {
//delete if space
// delete if space
if text == " " {
password[passwordIndex] = ""
} else {
Expand Down Expand Up @@ -156,7 +157,7 @@ public class SVPinView: UIView {
}
}

//MARK: Public methods
// MARK: Public methods
@objc
public func getPin() -> String {

Expand All @@ -178,7 +179,6 @@ public class SVPinView: UIView {
self.view.removeFromSuperview()
isLoading = true
loadView()
// self.collectionView.reloadData()
}

@objc
Expand Down Expand Up @@ -206,7 +206,7 @@ public class SVPinView: UIView {
}
})

//store text
// store text
password.append(String(char))
validateAndSendCallback()
}
Expand All @@ -226,7 +226,7 @@ extension SVPinView : UICollectionViewDataSource, UICollectionViewDelegate, UICo
let underLine = cell.viewWithTag(50)!
let placeholderLabel = cell.viewWithTag(400) as! UILabel

//Setting up textField
// Setting up textField
textField.tag = 101 + indexPath.row
textField.text = " "
textField.isSecureTextEntry = false
Expand Down Expand Up @@ -267,7 +267,12 @@ extension SVPinView : UICollectionViewDataSource, UICollectionViewDelegate, UICo
containerView.layer.borderColor = borderLineColor.cgColor
}

//Finished loading pinView
// Make the Pin field the first responder
if let firstResponderIndex = becomeFirstResponderAtIndex, firstResponderIndex == indexPath.item {
textField.becomeFirstResponder()
}

// Finished loading pinView
if indexPath.row == pinLength - 1 && isLoading {
isLoading = false
DispatchQueue.main.async {
Expand All @@ -284,19 +289,29 @@ extension SVPinView : UICollectionViewDataSource, UICollectionViewDelegate, UICo
return CGSize(width: width, height: collectionView.frame.height)
}
let width = (collectionView.bounds.width - (interSpace * CGFloat(max(pinLength, 1) - 1)))/CGFloat(pinLength)
return CGSize(width: width, height: width)
let height = collectionView.frame.height
return CGSize(width: min(width, height), height: min(width, height))
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return interSpace
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
let width = (collectionView.bounds.width - (interSpace * CGFloat(max(pinLength, 1) - 1)))/CGFloat(pinLength)
let top = (collectionView.bounds.height - width) / 2
let height = collectionView.frame.height
let top = (collectionView.bounds.height - min(width, height)) / 2
if height < width {
// If width of field > height, size the fields to the pinView height and center them.
let totalCellWidth = height * CGFloat(pinLength)
let totalSpacingWidth = interSpace * CGFloat(max(pinLength, 1) - 1)
let inset = (collectionView.frame.size.width - CGFloat(totalCellWidth + CGFloat(totalSpacingWidth))) / 2
return UIEdgeInsets(top: top, left: inset, bottom: 0, right: inset)
}
return UIEdgeInsets(top: top, left: 0, bottom: 0, right: 0)
}

Expand Down

0 comments on commit 36e1d88

Please sign in to comment.