CristiC
CristiC

Reputation: 22698

performSelectorOnMainThread is not waiting to finish

I have a thread which performs some tasks.

At the end I want to run a selector to hide an image with animation.

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

I am setting the duration to 10 seconds:

- (void) finishUpdate {

    UIImageView *myImage = (UIImageView *)[self.view viewWithTag:788];

    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:10.0f];
    myImage.frame = CGRectMake(0, 200, 320, 80);
    myImage.alpha = 0.0f;
    [UIView commitAnimations];
}

But it is disappearing instantly and the thread continues immediately.

Do you know how to make my thread to wait or how to display this simple animation?

Upvotes: 1

Views: 734

Answers (2)

CristiC
CristiC

Reputation: 22698

Instead of waiting for the selector to finish, I used sleepForTimeInterval: method to pause my thread, using the same duration as my animation.

[NSThread sleepForTimeInterval:0.3];

Upvotes: 2

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81868

It is normal behavior for the method to return immediately. [UIView commitAnimations] does not wait for the animation to finish (or any other method you use).

Kristopher Johnson is right that you shouldn't use UIGraphicsGetCurrentContext in this context. It is used in drawRect: or whenever there is a valid graphics context.

The animation context does not relate to a graphics context. Just pass NULL if you don't need any context in some animation delegate.

Upvotes: 2

Related Questions