Created
October 3, 2016 00:30
-
-
Save AnselmeKotchap/c245482be68b4c1782f900b29fc26dab to your computer and use it in GitHub Desktop.
some basic animation: shake, rotate, zoom in out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//shake animation: http://stackoverflow.com/questions/27987048/shake-animation-for-uitextfield-uiview-in-swift | |
public extension UIView { | |
func shake(count : Float? = nil,for duration : TimeInterval? = nil,withTanslation translation : Float? = nil) { | |
let animation : CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.x") | |
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) | |
animation.repeatCount = count ?? 2 | |
animation.duration = (duration ?? 0.5)/TimeInterval(animation.repeatCount) | |
animation.autoreverses = true | |
animation.byValue = translation ?? -5 | |
layer.add(animation, forKey: "shake") | |
} | |
} | |
//rotate: https://www.andrewcbancroft.com/2014/10/15/rotate-animation-in-swift/ | |
extension UIView { | |
func rotate360Degrees(duration: CFTimeInterval = 1.0, completionDelegate: AnyObject? = nil) { | |
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") | |
rotateAnimation.fromValue = 0.0 | |
rotateAnimation.toValue = CGFloat(M_PI * 2.0) | |
rotateAnimation.duration = duration | |
if let delegate: AnyObject = completionDelegate { | |
rotateAnimation.delegate = delegate | |
} | |
self.layer.addAnimation(rotateAnimation, forKey: nil) | |
} | |
} | |
//zoom http://stackoverflow.com/questions/31320819/scale-uibutton-animation-swift | |
extension UIView { | |
/** | |
Simply zooming in of a view: set view scale to 0 and zoom to Identity on 'duration' time interval. | |
- parameter duration: animation duration | |
*/ | |
func zoomIn(duration duration: NSTimeInterval = 0.2) { | |
self.transform = CGAffineTransformMakeScale(0.0, 0.0) | |
UIView.animateWithDuration(duration, delay: 0.0, options: [.CurveLinear], animations: { () -> Void in | |
self.transform = CGAffineTransformIdentity | |
}) { (animationCompleted: Bool) -> Void in | |
} | |
} | |
/** | |
Simply zooming out of a view: set view scale to Identity and zoom out to 0 on 'duration' time interval. | |
- parameter duration: animation duration | |
*/ | |
func zoomOut(duration duration: NSTimeInterval = 0.2) { | |
self.transform = CGAffineTransformIdentity | |
UIView.animateWithDuration(duration, delay: 0.0, options: [.CurveLinear], animations: { () -> Void in | |
self.transform = CGAffineTransformMakeScale(0.0, 0.0) | |
}) { (animationCompleted: Bool) -> Void in | |
} | |
} | |
/** | |
Zoom in any view with specified offset magnification. | |
- parameter duration: animation duration. | |
- parameter easingOffset: easing offset. | |
*/ | |
func zoomInWithEasing(duration duration: NSTimeInterval = 0.2, easingOffset: CGFloat = 0.2) { | |
let easeScale = 1.0 + easingOffset | |
let easingDuration = NSTimeInterval(easingOffset) * duration / NSTimeInterval(easeScale) | |
let scalingDuration = duration - easingDuration | |
UIView.animateWithDuration(scalingDuration, delay: 0.0, options: .CurveEaseIn, animations: { () -> Void in | |
self.transform = CGAffineTransformMakeScale(easeScale, easeScale) | |
}, completion: { (completed: Bool) -> Void in | |
UIView.animateWithDuration(easingDuration, delay: 0.0, options: .CurveEaseOut, animations: { () -> Void in | |
self.transform = CGAffineTransformIdentity | |
}, completion: { (completed: Bool) -> Void in | |
}) | |
}) | |
} | |
/** | |
Zoom out any view with specified offset magnification. | |
- parameter duration: animation duration. | |
- parameter easingOffset: easing offset. | |
*/ | |
func zoomOutWithEasing(duration duration: NSTimeInterval = 0.2, easingOffset: CGFloat = 0.2) { | |
let easeScale = 1.0 + easingOffset | |
let easingDuration = NSTimeInterval(easingOffset) * duration / NSTimeInterval(easeScale) | |
let scalingDuration = duration - easingDuration | |
UIView.animateWithDuration(easingDuration, delay: 0.0, options: .CurveEaseOut, animations: { () -> Void in | |
self.transform = CGAffineTransformMakeScale(easeScale, easeScale) | |
}, completion: { (completed: Bool) -> Void in | |
UIView.animateWithDuration(scalingDuration, delay: 0.0, options: .CurveEaseOut, animations: { () -> Void in | |
self.transform = CGAffineTransformMakeScale(0.0, 0.0) | |
}, completion: { (completed: Bool) -> Void in | |
}) | |
}) | |
} | |
} |
Great , Thank you. It's useful.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great