Commit 65c3210c by Daniel Dahan

issue-2: added completion block to motion animations

parent 48064570
......@@ -235,7 +235,7 @@ extension UIView {
/**
Snapshots the view instance for animations during transitions.
- Parameter afterUpdates: A boolean indicating whether to snapshot the view
after a render update, or as is.
after a render update, or as is.
- Parameter shouldHide: A boolean indicating whether the view should be hidden
after the snapshot is taken.
- Returns: A UIView instance that is a snapshot of the given UIView.
......@@ -243,6 +243,9 @@ extension UIView {
open func transitionSnapshot(afterUpdates: Bool, shouldHide: Bool = true) -> UIView {
isHidden = false
// Material specific.
(self as? PulseableLayer)?.pulseLayer?.isHidden = true
let oldCornerRadius = layer.cornerRadius
layer.cornerRadius = 0
......@@ -288,6 +291,9 @@ extension UIView {
v.motionTransform = motionTransform
v.backgroundColor = backgroundColor
// Material specific.
(self as? PulseableLayer)?.pulseLayer?.isHidden = false
isHidden = shouldHide
return v
......@@ -422,9 +428,9 @@ open class Motion: NSObject {
/**
An initializer to modify the presenting and container state.
- Parameter isPresenting: A boolean value indicating if the
- Parameter isPresenting: A boolean value indicating if the
Motion instance is presenting the view controller.
- Parameter isContainer: A boolean value indicating if the
- Parameter isContainer: A boolean value indicating if the
Motion instance is a container view controller.
*/
public init(isPresenting: Bool, isContainer: Bool) {
......@@ -469,7 +475,6 @@ open class Motion: NSObject {
if !$0 {
DispatchQueue.main.async(execute: block)
}
cancelable = nil
}
......@@ -524,7 +529,6 @@ extension Motion: UIViewControllerAnimatedTransitioning {
@objc(animateTransition:)
open func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
self.transitionContext = transitionContext
fromViewController.motionDelegate?.motion?(motion: self, willTransition: fromView, toView: toView)
Motion.delay(delayTransitionByTimeInterval) { [weak self] in
......@@ -658,7 +662,7 @@ extension Motion {
snapshotChildGroup.timingFunction = MotionAnimationTimingFunctionToValue(timingFunction: tf)
snapshot.animate(snapshotGroup)
snapshot.subviews.first!.animate(snapshotChildGroup)
snapshot.subviews.first?.animate(snapshotChildGroup)
}
}
}
......
......@@ -93,6 +93,9 @@ public enum MotionAnimation {
case size(width: CGFloat, height: CGFloat)
}
@available(iOS 10, *)
extension CALayer: CAAnimationDelegate {}
extension CALayer {
/**
......@@ -111,7 +114,10 @@ extension CALayer {
*/
open func animate(_ animations: [CAAnimation]) {
for animation in animations {
animation.delegate = self
if nil == animation.delegate {
animation.delegate = self
}
if let a = animation as? CABasicAnimation {
a.fromValue = (presentation() ?? self).value(forKeyPath: a.keyPath!)
}
......@@ -126,6 +132,8 @@ extension CALayer {
}
}
open func animationDidStart(_ anim: CAAnimation) {}
/**
A delegation function that is executed when the backing layer stops
running an animation.
......@@ -134,9 +142,9 @@ extension CALayer {
because it was completed or interrupted. True if completed, false
if interrupted.
*/
open func animationDidStop(_ animation: CAAnimation, finished flag: Bool) {
guard let a = animation as? CAPropertyAnimation else {
if let a = (animation as? CAAnimationGroup)?.animations {
open func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
guard let a = anim as? CAPropertyAnimation else {
if let a = (anim as? CAAnimationGroup)?.animations {
for x in a {
animationDidStop(x, finished: true)
}
......@@ -171,9 +179,10 @@ extension CALayer {
/**
A function that accepts an Array of MotionAnimation values and executes them.
- Parameter animations: An Array of MotionAnimation values.
- Parameter completion: An optional completion block.
*/
open func motion(_ animations: [MotionAnimation]) {
motion(delay: 0, duration: 0.35, timingFunction: .easeInEaseOut, animations: animations)
open func motion(_ animations: [MotionAnimation], completion: (() -> Void)? = nil) {
motion(delay: 0, duration: 0.35, timingFunction: .easeInEaseOut, animations: animations, completion: completion)
}
/**
......@@ -182,8 +191,9 @@ extension CALayer {
- Parameter duration: The animation duration TimeInterval.
- Parameter timingFunction: The animation MotionAnimationTimingFunction.
- Parameter animations: An Array of MotionAnimations.
- Parameter completion: An optional completion block.
*/
fileprivate func motion(delay: TimeInterval, duration: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: [MotionAnimation]) {
fileprivate func motion(delay: TimeInterval, duration: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: [MotionAnimation], completion: (() -> Void)? = nil) {
var t = delay
for v in animations {
......@@ -311,13 +321,16 @@ extension CALayer {
g.timingFunction = MotionAnimationTimingFunctionToValue(timingFunction: tf)
s.animate(g)
guard let execute = completion else {
return
}
Motion.delay(d, execute: execute)
}
}
}
@available(iOS 10, *)
extension CALayer: CAAnimationDelegate {}
extension UIView {
/// Computes the rotation of the view.
open var motionRotationAngle: CGFloat {
......@@ -385,9 +398,10 @@ extension UIView {
A function that accepts an Array of MotionAnimation values and executes
them on the view's backing layer.
- Parameter animations: An Array of MotionAnimation values.
- Parameter completion: An optional completion block.
*/
open func motion(_ animations: [MotionAnimation]) {
layer.motion(animations)
open func motion(_ animations: [MotionAnimation], completion: (() -> Void)? = nil) {
layer.motion(animations, completion: completion)
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment