How to pause and resume UIView Animation?

后端 未结 5 1373
长发绾君心
长发绾君心 2020-12-10 08:28

I have a UIView with several UILabels that is animating from top to bottom and vice versa. A sort of Autoque let\'s say :) I use 2 functions:

-(void)goUp 
-(         


        
5条回答
  •  心在旅途
    2020-12-10 09:16

    Vladimir, the question about CAAnimations make sense.. but I found a way to 'pause' so I could keep using UIView animations:

    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 using the presentationLayer to find out current x value of the animated view. After that I execute a new animation which overwrites the original animation. Make sure to setAnimationBeginsFromCurrentstate:YES for this. This cancels the original animation and puts the animated view not to it's destination position (which it does automatically ) but at the current position of the animation proces..

    Hope this helps others too! :)

提交回复
热议问题