How to pause and resume UIView animation?

I have a UIView with several UILabels that are animated from top to bottom and vice versa. Some kind of Autoque let say :) I am using 2 functions:

-(void)goUp -(void)goDown 

These functions trigger the UIView animation at the desired position. They have a specific AnimationDidStopSelector that calls another function at the end. It all works smoothly.

When touching the screen using touchhesBegan, I would like to pause the current animation and change the vertical position of the UIView using the touchtsMoved event. In touchesEnded I want to resume the animation to the desired end position.

What will be the correct way?

Thomas

+4
source share
5 answers

I created a category in UIView to pause and stop animations:

 @interface UIView (AnimationsHandler) - (void)pauseAnimations; - (void)resumeAnimations; @end @implementation UIView (AnimationsHandler) - (void)pauseAnimations { CFTimeInterval paused_time = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil]; self.layer.speed = 0.0; self.layer.timeOffset = paused_time; } - (void)resumeAnimations { CFTimeInterval paused_time = [self.layer timeOffset]; self.layer.speed = 1.0f; self.layer.timeOffset = 0.0f; self.layer.beginTime = 0.0f; CFTimeInterval time_since_pause = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil] - paused_time; self.layer.beginTime = time_since_pause; } 
+7
source

In fact, you can still pause the UIView animation based on the answer to a question related to Vladimir, since it pauses my CABasicAnimations as well as my UIView animations after I completed all my animations as CABasicaAnimations and then added some UIView animations. which, I thought, would not be suspended, but they also did not work. This is a relevant link .

I wanted to pause my entire view, so I went through self.view.layer as the layer to pause. But for those who don't know about CALayer , go to the view.layer you want to pause. Each UIView has a CALayer , so just go to the topmost view.layer that suits you. In the case of Thomas, based on your own answer, it seems like you want to go to self.containerView.layer to pause.

The reason for this is that UIView animation is just a layer on top of Core Animation. At least that's my understanding.

I hope this helps future people wonder how to pause animation.

+4
source

Vladimir, the question about CAAnimations makes sense ... but I found a way to “pause” so that I can continue to use the UIView animation:

 CALayer *pLayer = [self.containerView.layer presentationLayer]; CGRect frameStop = pLayer.frame; pausedX = frameStop.origin.x; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:0.01]; [UIView setAnimationCurve: UIViewAnimationCurveLinear]; // set view properties frameStop.origin.x = pausedX; self.containerView.frame = frameStop; [UIView commitAnimations]; 

What I'm doing here is to use the presentation layer to find out the current value x of the animated view. After that, I launch a new animation that overwrites the original animation. To do this, be sure to set the value of AnimationBeginsFromCurrentstate: YES. This cancels the original animation and places the animated view not in its destination (which it does automatically), but in the current position of the animation processes.

Hope this helps others too! :)

+2
source

I'm not sure if this is possible with UIView directly, but you can definitely make this animated view of CALayers. See this question about pausing and resuming CAAnimations.

+1
source

Hope this helps you.

 - (void)goUP{ CFTimeInterval pausedTime = [self.layer timeOffset]; self.layer.speed = 1.0; self.layer.timeOffset = 0.0; self.layer.beginTime = 0.0; CFTimeInterval timeSincePause = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime; self.layer.beginTime = timeSincePause; } - (void)goDown{ CFTimeInterval pausedTime = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil]; self.layer.speed = 0.0; self.layer.timeOffset = pausedTime; } 

When you invoke a Layer animation, it will affect the animation of all tree layers and subframes.

0
source

Source: https://habr.com/ru/post/1315172/


All Articles