Control a UIView animation's progress manually

2019-03-20 14:14发布

I want to setup a UIView animation, but instead of it taking a given amount of time, i want to control the percentage progress and let it automatically interpolate the values for me. Is that possible? Thanks

2条回答
一夜七次
2楼-- · 2019-03-20 14:20

I think i've got it. Immediately after the [UIView animate...] block, do the following on the root view:

_view.layer.speed = 0.0;
_view.layer.timeOffset = [_view.layer convertTime:CACurrentMediaTime() fromLayer:nil];

And to set the point on the scale, do:

_view.layer.timeOffset = 0..1; (where 1 is the same value as the original duration)

And to resume, something similar to the following:

CALayer *layer = _view.layer;
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;

This is all from here: https://developer.apple.com/library/ios/qa/qa1673/_index.html

查看更多
倾城 Initia
3楼-- · 2019-03-20 14:28

If you're talking about a simple UIView animation linked to some gesture recognizer, you generally would not use animation during the gesture, but rather just update the view's properties as you receive updates as the continuous gesture (e.g. UIPanGestureRecognizer) progresses, and then only apply a traditional animation when you let go and want complete the animation that the user initiated manually. You don't need to employ animation during the continuous gesture because those events come in quickly enough to render a smooth changing of the view's properties during the gesture. You only need traditional animation when the user lets go and you want to continue the animation smoothly to its logical conclusion.

Alternatively, if you're talking about the new iOS 7 view controller transition (a slightly complicated, very specialized scenario), you (a) set up an animator object (one that conforms to UIViewControllerAnimatedTransitioning); (b) set up an interaction controller (on that conforms to UIViewControllerInteractiveTransitioning, e.g. a UIPercentDrivenInteractiveTransition); (c) your gesture would update the interaction controller's percentComplete; and (d) when the gesture is done, you'd simply call either cancelInteractiveTransition or finishInteractiveTransition based upon your own logic.

查看更多
登录 后发表回答