Skip to content

Instantly share code, notes, and snippets.

@soggybag
Last active August 2, 2016 20:27
Show Gist options
  • Save soggybag/f6c6eed0a2a95677a15e to your computer and use it in GitHub Desktop.
Save soggybag/f6c6eed0a2a95677a15e to your computer and use it in GitHub Desktop.
Swift UIView Extension - Animation helper
import UIKit
extension UIViewController {
func animateThing(thing: UIView, offsetX: CGFloat, offsetY: CGFloat, alpha: CGFloat, time: NSTimeInterval, delay: NSTimeInterval) {
let targetY = thing.center.y
let targetX = thing.center.x
let targetAlpha = thing.alpha
thing.center.y = thing.center.y - offsetY
thing.center.x = thing.center.x - offsetX
thing.alpha = thing.alpha - alpha
UIView.animateWithDuration(time, delay: delay, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
thing.center.y = targetY
thing.center.x = targetX
thing.alpha = targetAlpha
}, completion: nil)
}
}
// use the code above like this:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//
animateThing(self.label, offsetY: 120, offsetX: 0, time: 1.0, delay: 0.0)
animateThing(self.button, offsetY: 200, offsetX: -100, time: 2.0, delay: 1.0)
animateThing(self.box, offsetY: 0, offsetX: -400, time: 1.0, delay: 4.0)
//
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment