UIView animation block pause both the animation and the completion code

后端 未结 1 1956
猫巷女王i
猫巷女王i 2021-02-15 14:21

I have an animation block to perform a simple transform based animation, that on completion removes the view in question from its superview.

UIView *msgView = [[         


        
相关标签:
1条回答
  • 2021-02-15 15:17

    As I said in the comments above, there doesn't seem to be a problem. The below is tested in iOS 5.1 and 6.1.

    Create a storyboard with UIImageView *transView and UIButton *trigger. Here's the class:

    TSTViewController.h:

    @property (weak, nonatomic) IBOutlet UIImageView *transView;
    @property (weak, nonatomic) IBOutlet UIButton *trigger;
    @property (nonatomic) NSUInteger bState;
    
    - (IBAction)didPressTrigger:(id)sender;
    

    TSTViewController.m:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.bState = 0;    // 0 is initial state
                            // 1 is transform being animated
                            // 2 is transform paused
                            // 3 is transform ended
    }
    
    - (IBAction)didPressTrigger:(id)sender {
        switch (self.bState) {
            case 0:
            {
                CATransform3D transform = CATransform3DMakeScale(2.5, 2.5, 1.0);
                self.bState++;
                [UIView animateWithDuration:5.0
                                 animations:^(void){self.transView.layer.transform = transform;}
                                 completion:^(BOOL finished){
                                     self.bState = 3;
                                     NSLog(@"Done");
                                 }];
                break;
            }
            case 1:
            {
                self.bState++;
                [self pauseLayer:self.transView.layer];
                break;
            }
            case 2:
            {
                self.bState = 1;
                [self resumeLayer:self.transView.layer];
                break;
            }
            case 3:
            {
                [UIView animateWithDuration:0 animations:^(void){self.transView.layer.transform = CATransform3DIdentity;}
                                 completion:^(BOOL finished) {
                                     self.bState = 0;
                                     NSLog(@"Reset");
                                 }];
                break;
            }
            default:
                break;
        }
    }
    
    -(void)pauseLayer:(CALayer*)layer
    {
        CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
        layer.speed = 0.0;
        layer.timeOffset = pausedTime;
    }
    
    -(void)resumeLayer:(CALayer*)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;
    }
    

    When you press the trigger button, the animation starts. Press it again, the animation stops. Wait 10 seconds, and press the button yet again. The animation continues and finishes, and it logs "Done".

    0 讨论(0)
提交回复
热议问题