Created
January 6, 2015 20:01
-
-
Save WeZZard/f42b1ca72dc1c2759edf to your computer and use it in GitHub Desktop.
Create CASpringAnimation instance without accessing undocumented APIs
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
import UIKit | |
private let SharedCASpringAnimationFactory = CASpringAnimationFactory() | |
public class CASpringAnimationFactory { | |
private var dummyView: UIView | |
private init() { | |
dummyView = UIView(frame: CGRect.zeroRect) | |
} | |
private class var shared: CASpringAnimationFactory { | |
return SharedCASpringAnimationFactory | |
} | |
public class func animation(#keyPath: String, dumping: CGFloat, initialSpringVelocity: CGFloat) -> CABasicAnimation { | |
UIView.animateWithDuration(0.3, delay: 0.0, usingSpringWithDamping: dumping, initialSpringVelocity: initialSpringVelocity, options: nil, | |
animations: { () -> Void in | |
CASpringAnimationFactory.shared.dummyView.bounds = CGRect(origin: CGPoint.zero, size: CGSize(width: 100, height: 100)) | |
}, completion: nil) | |
let arrestedAnimation = shared.dummyView.layer.animationForKey("bounds").copy() as CABasicAnimation | |
arrestedAnimation.duration = CATransaction.animationDuration() | |
arrestedAnimation.keyPath = keyPath | |
arrestedAnimation.fromValue = nil | |
arrestedAnimation.toValue = nil | |
shared.dummyView.layer.removeAllAnimations() | |
shared.dummyView.bounds = CGRect.zeroRect | |
return arrestedAnimation | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment