Created
August 12, 2016 14:47
-
-
Save MattesGroeger/10f8a7f8c65fbe00189ae774cfd32437 to your computer and use it in GitHub Desktop.
This Swift extension allows to use more timing functions than the default ones provided by `CAMediaTimingFunction`. It's based on the work of Christian Giordano: https://gist.github.com/nuthinking/7415509
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 QuartzCore | |
enum BezierType { | |
case Default | |
case Linear | |
case EaseIn | |
case EaseOut | |
case EaseInOut | |
case SineIn | |
case SineOut | |
case SineInOut | |
case QuadIn | |
case QuadOut | |
case QuadInOut | |
case CubicIn | |
case CubicOut | |
case CubicInOut | |
case QuartIn | |
case QuartOut | |
case QuartInOut | |
case QuintIn | |
case QuintOut | |
case QuintInOut | |
case ExpoIn | |
case ExpoOut | |
case ExpoInOut | |
case CircIn | |
case CircOut | |
case CircInOut | |
case BackIn | |
case BackOut | |
case BackInOut | |
} | |
extension CAMediaTimingFunction { | |
convenience init(type: BezierType) { | |
switch type { | |
case .Default: self.init(name: kCAMediaTimingFunctionDefault) | |
case .Linear: self.init(name: kCAMediaTimingFunctionLinear) | |
case .EaseIn: self.init(name: kCAMediaTimingFunctionEaseIn) | |
case .EaseOut: self.init(name: kCAMediaTimingFunctionEaseOut) | |
case .EaseInOut: self.init(name: kCAMediaTimingFunctionEaseInEaseOut) | |
case .SineIn: self.init(controlPoints: 0.45, 0, 1, 1) | |
case .SineOut: self.init(controlPoints: 0, 0, 0.55, 1) | |
case .SineInOut: self.init(controlPoints: 0.45, 0, 0.55, 1) | |
case .QuadIn: self.init(controlPoints: 0.43, 0, 0.82, 0.60) | |
case .QuadOut: self.init(controlPoints: 0.18, 0.4, 0.57, 1) | |
case .QuadInOut: self.init(controlPoints: 0.43, 0, 0.57, 1) | |
case .CubicIn: self.init(controlPoints: 0.67, 0, 0.84, 0.54) | |
case .CubicOut: self.init(controlPoints: 0.16, 0.46, 0.33, 1) | |
case .CubicInOut: self.init(controlPoints: 0.65, 0, 0.35, 1) | |
case .QuartIn: self.init(controlPoints: 0.81, 0, 0.77, 0.34) | |
case .QuartOut: self.init(controlPoints: 0.23, 0.66, 0.19, 1) | |
case .QuartInOut: self.init(controlPoints: 0.81, 0, 0.19, 1) | |
case .QuintIn: self.init(controlPoints: 0.89, 0, 0.81, 0.27) | |
case .QuintOut: self.init(controlPoints: 0.19, 0.73, 0.11, 1) | |
case .QuintInOut: self.init(controlPoints: 0.9, 0, 0.1, 1) | |
case .ExpoIn: self.init(controlPoints: 1.04, 0, 0.88, 0.49) | |
case .ExpoOut: self.init(controlPoints: 0.12, 0.51, -0.4, 1) | |
case .ExpoInOut: self.init(controlPoints: 0.95, 0, 0.05, 1) | |
case .CircIn: self.init(controlPoints: 0.6, 0, 1, 0.45) | |
case .CircOut: self.init(controlPoints: 1, 0.55, 0.4, 1) | |
case .CircInOut: self.init(controlPoints: 0.82, 0, 0.18, 1) | |
case .BackIn: self.init(controlPoints: 0.77, -0.63, 1, 1) | |
case .BackOut: self.init(controlPoints: 0, 0, 0.23, 1.37) | |
case .BackInOut: self.init(controlPoints: 0.77, -0.63, 0.23, 1.37) | |
} | |
} | |
} |
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
let animateOpacity = CABasicAnimation(keyPath: "opacity") | |
animateOpacity.duration = 0.3 | |
animateOpacity.timingFunction = CAMediaTimingFunction(type: .ExpoOut) | |
layer.opacity = 1.0 | |
layer.removeAllAnimations() | |
layer.addAnimation(animateOpacity, forKey: "opacity") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment