Skip to content

Instantly share code, notes, and snippets.

@WeZZard
Created January 6, 2015 20:01
Show Gist options
  • Save WeZZard/f42b1ca72dc1c2759edf to your computer and use it in GitHub Desktop.
Save WeZZard/f42b1ca72dc1c2759edf to your computer and use it in GitHub Desktop.
Create CASpringAnimation instance without accessing undocumented APIs
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