Alex Stone
Alex Stone

Reputation: 47354

iPhone iOS what is the correct way to do actions with UIView animateWithDuration block?

I'm wandering what is the correct way to do "long" actions in response to user events. For example, I have this slide to cancel block that animates itself off screen over 0.5 seconds. The [self coreDataAction] may take about 0.3 seconds itself.

I want to ensure that the action completes once the user sees the end of the animation ( I do not want the user to accidentally navigate to a different controller or close the app thinking that the action is done).

Where should I put the [self coreDataAction]; in this case? Above the block, within the block or in the completion block?

//should I put it here?

 CGPoint slideToCancelCenter = slideToCancel.view.center;
    [UIView animateWithDuration:0.5 animations:^{
        self.goToSleepButton.center = slideToCancelCenter;
        [UIView setAnimationDuration:0.5];
        CGPoint sliderCenter = slideToCancel.view.center;
        sliderCenter.y += slideToCancel.view.bounds.size.height;
        slideToCancel.view.center = sliderCenter;

//should I put it here?        
// [self coreDataAction];
    } completion:^(BOOL finished) {
//should I put it here?
    } ];

Upvotes: 2

Views: 203

Answers (2)

Beltalowda
Beltalowda

Reputation: 4678

A better way to handle this might be to animate the view on-screen then start the coreDataAction in the completion handler. Once the coreDataAction method execution is complete you can call a method to animate the slide to cancel view off-screen.

Upvotes: 2

Edwin Iskandar
Edwin Iskandar

Reputation: 4119

Assuming [self coreDataAction] executes on the main thread, I would say you should put it on the first line to ensure that the method is complete by the time the animation is done.

Upvotes: 1

Related Questions