Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control a UIView animation's progress manually instead of automatically through the time duration

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

like image 691
Chris Avatar asked Mar 26 '14 05:03

Chris


2 Answers

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

like image 152
Chris Avatar answered Sep 21 '22 23:09

Chris


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.

like image 21
Rob Avatar answered Sep 20 '22 23:09

Rob