Skip to content

Instantly share code, notes, and snippets.

@rtking1993
Last active January 31, 2018 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtking1993/a7b9b24cec94ee50cc4d8a2d6c68c617 to your computer and use it in GitHub Desktop.
Save rtking1993/a7b9b24cec94ee50cc4d8a2d6c68c617 to your computer and use it in GitHub Desktop.
Animations with UIViewAnimationOptions
import UIKit
// MARK: RaceViewController
class RaceViewController: UIViewController {
// MARK: Outlets
@IBOutlet weak var raceButton: UIButton!
@IBOutlet weak var raceView: UIView!
@IBOutlet weak var purpleCar: UIImageView!
@IBOutlet weak var greyCar: UIImageView!
@IBOutlet weak var yellowCar: UIImageView!
@IBOutlet weak var purpleCarTopConstraint: NSLayoutConstraint!
@IBOutlet weak var greyCarTopConstraint: NSLayoutConstraint!
@IBOutlet weak var yellowCarTopConstraint: NSLayoutConstraint!
// MARK: Constants
let duration = 4.0 // Duration of the animation
let delay = 0.0 // Delay of the animation
let carHeight = 100 // Height of the race cars
// MARK: Action Methods
@IBAction func startRace(button: UIButton) {
let distance = raceView.frame.height - CGFloat(carHeight + 20)
self.view.layoutIfNeeded()
// Purple car
UIView.animate(withDuration: duration, delay: delay, options: .curveEaseIn, animations: {
self.purpleCarTopConstraint.constant = distance
self.view.layoutIfNeeded()
}, completion: nil)
// Grey car
UIView.animate(withDuration: duration, delay: delay, animations: {
self.greyCarTopConstraint.constant = distance
self.view.layoutIfNeeded()
}, completion: nil)
// Yellow car
UIView.animate(withDuration: duration, delay: delay, options: .curveEaseOut, animations: {
self.yellowCarTopConstraint.constant = distance
self.view.layoutIfNeeded()
}, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment