cancel a UIView animateWithDuration before completion

后端 未结 3 1852
轮回少年
轮回少年 2021-02-12 16:30

I have this code in my project:

- (void) fadeImageView {
    [UIView animateWithDuration:1.0f
                          delay:0
                        options:         


        
3条回答
  •  难免孤独
    2021-02-12 17:27

    First of all you have to add UIViewAnimationOptionAllowUserInteraction to option like..

    - (void) fadeImageView {
        [UIView animateWithDuration:1.0f
                              delay:0
                            options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
                         animations:^{
                             self.imageView.alpha = 0.0f;
                         }
                         completion:^(BOOL finished) {
                             //make the image view un-tappable.
                             //if the fade was canceled, set the alpha to 1.0
                         }];
    
    }
    

    and then make a method like this....

    -(void)stopAnimation {
        [self.routeView.layer removeAllAnimations];
    }
    

    after that when you want to remove your animation call above method using .....

    [self performSelectorOnMainThread:@selector(stopAnimation) withObject:nil waitUntilDone:YES];
    

    Hope it will help you

    Happy coding.........!!!!!!!!!!!! :)

    EDIT:

    Thanks user1244109 to guide me for this.

    For iOS7 we have to add one more option UIViewAnimationOptionBeginFromCurrentState like:

    [UIView animateWithDuration:1.0f
                                  delay:0
                                options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState
                             animations:^{
                                 self.imageView.alpha = 0.0f;
                             }
                             completion:^(BOOL finished) {
                                 //make the image view un-tappable.
                                 //if the fade was canceled, set the alpha to 1.0
                             }];
    

提交回复
热议问题