Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

ios - How to cancel UIViews block-based animation?

i currently focus the following problem:

i start an animation, where 2 objects-attributes are triggered.

the code is:

    [UIView animateWithDuration:0.3 animations:^{
        greyscaleImage.alpha     = 1;
        activityIndicator.alpha  = 1;
    } completion:^(BOOL f){
        if(f)
        {
            [activityIndicator startAnimating];
        }
    }];

which works fine.

the only problem i discovered is, that i have a 0.3 seconds change to crash the app when the view which holds this activityIndicator and greyscaleImage is deallocated.

To make it more clear please imagine a ViewController, its view presented via default iOS-modal-View ways. Now trigger that animation, which takes 2 minutes. before reaching that 2 minutes, you find that animation is quite boring and you want to dismiss that view. now, that the view, activityIndicator and greyscaleImage are released, the animation o/c cannot know what to do.

so i wonder, what to do here + why the debugger points to

  } completion:^(BOOL f){

instead of e.g. [activityIndicator ...

is there a way, to allow user to dismiss the view before the 2 minutes are over?

Best Regards

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you start a new animation that takes 0.0 seconds and goes to the state you want to go to, it will cancel the old one and start the new (instant) 'animation'.

Example for when you want to stop a moving view by going to the place it already is at:

[UIView animateWithDuration:0.0
                      delay:0.0
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{movingView.frame = ((CALayer *)movingView.layer.presentationLayer).frame;}
                 completion:^(BOOL finished){}
 ];

options:UIViewAnimationOptionBeginFromCurrentState is important. Not calling it will let your animation start at the end state of the previous animation. In movement, it would warp to the end location before warping to the place you want it to stop at. Even though your cancel-'animation' is instant, the jumping back and forth may be visible.

Note: The animation time doesn't have to be 0.0 seconds, any animation will cancel the old one. Not entirely sure about different types of animations though. For example, I don't know if changing a frame would stop a fade.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...