Commit 4bf570ca by Orkhan Alikhanov

Fixed ~0.001 seconds precision issue by storing current time

The ~0.001 seconds lag between animations were extremely
noticeable if the duration was long.
parent df7f3727
......@@ -44,11 +44,6 @@ internal class MotionAnimatorViewContext {
/// Animation duration time.
var duration: TimeInterval = 0
/// The computed current time of the snapshot layer.
var currentTime: TimeInterval {
return snapshot.layer.convertTime(CACurrentMediaTime(), from: nil)
}
/// A container view for the transition.
var container: UIView? {
return animator?.motion.context.container
......
......@@ -35,6 +35,11 @@ internal class MotionCoreAnimationViewContext: MotionAnimatorViewContext {
/// A reference to the animation timing function.
fileprivate var timingFunction = CAMediaTimingFunction.standard
/// The computed current time of the snapshot layer.
var currentTime: TimeInterval {
return snapshot.layer.convertTime((animator as! MotionCoreAnimator<MotionCoreAnimationViewContext>).currentTime, from: nil)
}
/// Current animations.
var animations = [(CALayer, String, CAAnimation)]()
......
......@@ -29,6 +29,29 @@
import UIKit
internal class MotionCoreAnimator<T: MotionAnimatorViewContext>: MotionAnimator {
/**
Backing field for storing CACurrentMediaTime to ensure all
animations begin at the exact same time.
Should be invalidated using invalidateCurrentTime method
after firing all animations.
*/
private var storedCurrentTime: TimeInterval?
/// Current time for the animator.
var currentTime: TimeInterval {
if nil == storedCurrentTime {
storedCurrentTime = CACurrentMediaTime()
}
return storedCurrentTime!
}
/// Invalidates stored current time.
func invalidateCurrentTime() {
storedCurrentTime = nil
}
weak public var motion: MotionTransition!
/// A reference to the MotionContext.
......@@ -46,6 +69,7 @@ internal class MotionCoreAnimator<T: MotionAnimatorViewContext>: MotionAnimator
}
viewToContexts.removeAll()
invalidateCurrentTime()
}
/**
......@@ -103,6 +127,8 @@ internal class MotionCoreAnimator<T: MotionAnimatorViewContext>: MotionAnimator
d = max(d, v.startAnimations())
}
invalidateCurrentTime()
return d
}
......@@ -114,6 +140,8 @@ internal class MotionCoreAnimator<T: MotionAnimatorViewContext>: MotionAnimator
for v in viewToContexts.values {
v.seek(to: progress)
}
invalidateCurrentTime()
}
/**
......@@ -130,6 +158,7 @@ internal class MotionCoreAnimator<T: MotionAnimatorViewContext>: MotionAnimator
duration = max(duration, v.resume(at: progress, isReversed: isReversed))
}
invalidateCurrentTime()
return duration
}
......@@ -144,6 +173,8 @@ internal class MotionCoreAnimator<T: MotionAnimatorViewContext>: MotionAnimator
}
v.apply(state: state)
invalidateCurrentTime()
}
}
......
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