Skip to content

Instantly share code, notes, and snippets.

@m4rr
Created February 26, 2017 21:02
Show Gist options
  • Save m4rr/f030eb90f2d8f39b4a836f1a651f5d03 to your computer and use it in GitHub Desktop.
Save m4rr/f030eb90f2d8f39b4a836f1a651f5d03 to your computer and use it in GitHub Desktop.
Shake Fields
private func shakeFieldsAsError() {
errorFeedback()
[emailTextField, passwordTextField]
.enumerated()
.forEach { (index: Int, element: UITextField?) in
let c = element!.center
let ti = TimeInterval(index) * 0.05 // every next field is delayed from previous on 0.05 secs
let dur: Double = 0.25
let times = 5
let xOffset: CGFloat = 15
UIView.animateKeyframes(
withDuration: dur / 2 * Double(times),
delay: ti,
options: [.allowUserInteraction, .calculationModePaced],
animations: {
for i in 0 ..< times {
UIView.addKeyframe(withRelativeStartTime: dur * Double(i), relativeDuration: dur, animations: {
if i == times - 1 {
element?.center = c
} else {
let direction: CGFloat = i % 2 == 0 ? 1 : -1
element?.center.x = c.x + xOffset * direction
}
})
}
},
completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment