Skip to content

Instantly share code, notes, and snippets.

@vialyx
Created November 6, 2018 18:53
Show Gist options
  • Save vialyx/d0d29dba72ca8dabb36ca514e3874e22 to your computer and use it in GitHub Desktop.
Save vialyx/d0d29dba72ca8dabb36ca514e3874e22 to your computer and use it in GitHub Desktop.
@IBAction func buttonDidTap(_ sender: Any) {
// Check text in field. Animation is starting only if text is empty
if field.text?.isEmpty ?? true {
UIView.animate(withDuration: 0.3,
delay: 0.0,
// Look options list here https://developer.apple.com/documentation/uikit/uiviewanimationoptions
options: [.autoreverse],
animations: {
// Create CGAffineTransform with 5 degree (CG works with radians) and convert in to radians
self.field.transform = CGAffineTransform(rotationAngle: CGFloat(5).degreesToRadians)
// Add scale factor to the created transform
.scaledBy(x: 1.2, y: 1.2)
}) { (finish) in
// Clear transform .identity is a empty transform
self.field.transform = .identity
print("\(String(describing: self.field)) animation finish: \(finish)")
}
} else {
// Start Repeat Animation for UILabel
UIView.animate(withDuration: 0.5,
delay: 0.0,
// .repeat option create loop for that animation
options: [.repeat, .autoreverse],
animations: {
// Concat static text with text from UITextField
self.label.text = "Entered name: " + (self.field.text ?? "")
// Make offset from UILabel frame
self.label.frame = self.label.frame.offsetBy(dx: 100, dy: 0)
}) { (finish) in
print("\(String(describing: self.label)) animation finish: \(finish)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment