Commit 147da727 by Daniel Dahan

reworked the MotionViewPropertyViewContext class

parent 1bce1231
...@@ -56,6 +56,7 @@ extension MotionDefaultAnimator { ...@@ -56,6 +56,7 @@ extension MotionDefaultAnimator {
} }
extension MotionDefaultAnimator { extension MotionDefaultAnimator {
/// Cleans the contexts.
func clean() { func clean() {
for v in viewToContexts.values { for v in viewToContexts.values {
v.clean() v.clean()
...@@ -65,6 +66,12 @@ extension MotionDefaultAnimator { ...@@ -65,6 +66,12 @@ extension MotionDefaultAnimator {
insertToViewFirst = false insertToViewFirst = false
} }
/**
A function that determines if a view can be animated.
- Parameter view: A UIView.
- Parameter isAppearing: A boolean that determines whether the
view is appearing.
*/
func canAnimate(view: UIView, isAppearing: Bool) -> Bool { func canAnimate(view: UIView, isAppearing: Bool) -> Bool {
guard let state = context[view] else { guard let state = context[view] else {
return false return false
...@@ -73,6 +80,12 @@ extension MotionDefaultAnimator { ...@@ -73,6 +80,12 @@ extension MotionDefaultAnimator {
return T.canAnimate(view: view, state: state, isAppearing: isAppearing) return T.canAnimate(view: view, state: state, isAppearing: isAppearing)
} }
/**
Animates the from-views to the to-views.
- Parameter fromViews: An Array of UIViews.
- Parameter toViews: An Array of UIViews.
- Returns: A TimeInterval.
*/
func animate(fromViews: [UIView], toViews: [UIView]) -> TimeInterval { func animate(fromViews: [UIView], toViews: [UIView]) -> TimeInterval {
var duration: TimeInterval = 0 var duration: TimeInterval = 0
...@@ -102,12 +115,23 @@ extension MotionDefaultAnimator { ...@@ -102,12 +115,23 @@ extension MotionDefaultAnimator {
return duration return duration
} }
/**
Moves the view's animation to the given elapsed time.
- Parameter to elapsedTime: A TimeInterval.
*/
func seek(to elapsedTime: TimeInterval) { func seek(to elapsedTime: TimeInterval) {
for v in viewToContexts.values { for v in viewToContexts.values {
v.seek(to: elapsedTime) v.seek(to: elapsedTime)
} }
} }
/**
Resumes the animation with a given elapsed time and
optional reversed boolean.
- Parameter at elapsedTime: A TimeInterval.
- Parameter isReversed: A boolean to reverse the animation
or not.
*/
func resume(at elapsedTime: TimeInterval, isReversed: Bool) -> TimeInterval { func resume(at elapsedTime: TimeInterval, isReversed: Bool) -> TimeInterval {
var duration: TimeInterval = 0 var duration: TimeInterval = 0
...@@ -119,6 +143,11 @@ extension MotionDefaultAnimator { ...@@ -119,6 +143,11 @@ extension MotionDefaultAnimator {
return duration return duration
} }
/**
Applies the given state to the given view.
- Parameter state: A MotionTargetState.
- Parameter to view: A UIView.
*/
func apply(state: MotionTargetState, to view: UIView) { func apply(state: MotionTargetState, to view: UIView) {
guard let v = viewToContexts[view] else { guard let v = viewToContexts[view] else {
return return
......
...@@ -30,14 +30,14 @@ import UIKit ...@@ -30,14 +30,14 @@ import UIKit
@available(iOS 10, tvOS 10, *) @available(iOS 10, tvOS 10, *)
internal class MotionViewPropertyViewContext: MotionAnimatorViewContext { internal class MotionViewPropertyViewContext: MotionAnimatorViewContext {
/// A reference to the UIViewPropertyAnimator.
var viewPropertyAnimator: UIViewPropertyAnimator? fileprivate var viewPropertyAnimator: UIViewPropertyAnimator?
override class func canAnimate(view: UIView, state: MotionTargetState, isAppearing: Bool) -> Bool { override class func canAnimate(view: UIView, state: MotionTargetState, isAppearing: Bool) -> Bool {
return view is UIVisualEffectView && state.opacity != nil return view is UIVisualEffectView && nil != state.opacity
} }
override func resume(elapsedTime: TimeInterval, isReversed: Bool) { override func resume(at elapsedTime: TimeInterval, isReversed: Bool) {
viewPropertyAnimator?.finishAnimation(at: isReversed ? .start : .end) viewPropertyAnimator?.finishAnimation(at: isReversed ? .start : .end)
} }
...@@ -53,15 +53,20 @@ internal class MotionViewPropertyViewContext: MotionAnimatorViewContext { ...@@ -53,15 +53,20 @@ internal class MotionViewPropertyViewContext: MotionAnimatorViewContext {
} }
override func startAnimations(isAppearing: Bool) { override func startAnimations(isAppearing: Bool) {
guard let visualEffectView = snapshot as? UIVisualEffectView else { return } guard let v = snapshot as? UIVisualEffectView else {
let appearedEffect = visualEffectView.effect return
let disappearedEffect = targetState.opacity == 0 ? nil : visualEffectView.effect }
visualEffectView.effect = isAppearing ? disappearedEffect : appearedEffect
let appearedEffect = v.effect
let disappearedEffect = 0 == targetState.opacity ? nil : v.effect
v.effect = isAppearing ? disappearedEffect : appearedEffect
duration = targetState.duration! duration = targetState.duration!
viewPropertyAnimator = UIViewPropertyAnimator(duration: duration, curve: .easeInOut) { viewPropertyAnimator = UIViewPropertyAnimator(duration: duration, curve: .easeInOut) {
visualEffectView.effect = isAppearing ? appearedEffect : disappearedEffect v.effect = isAppearing ? appearedEffect : disappearedEffect
} }
viewPropertyAnimator!.startAnimation()
viewPropertyAnimator?.startAnimation()
} }
} }
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