简体   繁体   中英

Reusable UIView Animation

Consider this basic view animation. It will scale a view down to nothing over the course of 1 second:

[UIView animateWithDuration: 1.0
                      delay: 0.0
                    options: UIViewAnimationCurveEaseIn
                 animations:^{
                     vw.transform = CGAffineTransformMakeScale(0.0, 0.0);
                 }
                 completion:^(BOOL finished) {
                     if (finished) {
                         vw.transform = CGAffineTransformIdentity;
                     }
                 }
];

What I'd like to do is use this same animation on many different views at different times throughout my app. To do this, I can easily put it in a method, pass in the view I want to zoom. No problem.

The challenge is that I may need to do different things at the completion of the animation depending on the situation. For example, I may want to remove the view from the SuperView in one case, and I may want to move the view to a new location (for zooming in later) in another case.

In a nutshell: How can I notify the caller of my method when the animation completes?

completion:^(BOOL finished) {
    if (finished) {
        vw.transform = CGAffineTransformIdentity;
        // Do something unique here
    }
}

You can just pass in the completion block itself as well:

- (void)animateView:(UIView *)v completion:(void ^(BOOL))ch
{
    [UIView animateWithDuration: 1.0
                          delay: 0.0
                        options: UIViewAnimationCurveEaseIn
                     animations:^{
                         v.transform = CGAffineTransformMakeScale(0.0, 0.0);
                     }
                     completion:ch
    ];
}

Then call it like

[self animateView:someView completion:^(BOOL flag) {
    // do stuff here
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM