Created
January 24, 2016 18:04
-
-
Save psparago/25634711db83a7eaa6e3 to your computer and use it in GitHub Desktop.
Simple UIView bounce animation
This file contains 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
extension UIView { | |
private class AnimationDelegate : NSObject { | |
let completion: (() -> Void)? | |
init(completion: (() -> Void)?) { | |
self.completion = completion | |
} | |
override func animationDidStop(theAnimation: CAAnimation, finished flag: Bool) { | |
if completion != nil { | |
completion!() | |
} | |
} | |
} | |
func bounceView(completion: (() -> Void)? = nil) { | |
let animation = bounceAnimation(frame.height) | |
animation.delegate = AnimationDelegate(completion: completion) | |
layer.addAnimation(animation, forKey: "bounce") | |
} | |
func bounceAnimation(itemHeight: CGFloat) -> CAKeyframeAnimation { | |
let factors: [CGFloat] = [0, 32, 60, 83, 100, 114, 124, 128, 128, 124, 114, 100, 83, 60, 32, | |
0, 24, 42, 54, 62, 64, 62, 54, 42, 24, 0, 18, 28, 32, 28, 18, 0] | |
var transforms: [AnyObject] = [] | |
for factor in factors { | |
let positionOffset = factor/256.0 * itemHeight | |
var transform = CATransform3DMakeTranslation(0, -positionOffset, 0) | |
transform = CATransform3DConcat(self.layer.transform, transform) | |
transforms.append(NSValue(CATransform3D: transform)) | |
} | |
let animation = CAKeyframeAnimation(keyPath: "transform") | |
animation.repeatCount = 1 | |
animation.duration = CFTimeInterval(factors.count)/30.0 | |
animation.fillMode = kCAFillModeForwards | |
animation.values = transforms | |
animation.removedOnCompletion = true // final stage is equal to starting stage | |
animation.autoreverses = false | |
return animation; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment