views:

13

answers:

1

Hi all. I've managed to implement a solution to this problem, however the code just seems rather inefficient and actually, the delayed method calls are proving a little troublesome when they continue to fire if the user has navigated to another screen.

Basically I want a thought bubble to appear from a character's head, but animated, so that a small bubble appears, followed by a larger one, a larger one still and eventually the main bubble which then contains a little animation. I've managed this by displaying each bubble with its own method, and then calling the methods with delays:

- (void)showMood {
    animating = TRUE;
    [self showBubble1];
}

- (void)showBubble1 {
    bubble1.hidden = FALSE;
    [self performSelector:@selector(showBubble2) withObject:nil afterDelay:kBubbleDelay];
}

- (void)showBubble2 {
    bubble2.hidden = FALSE;
    [self performSelector:@selector(showBubble3) withObject:nil afterDelay:kBubbleDelay];
}

- (void)showBubble3 {
    bubble3.hidden = FALSE;
    [self performSelector:@selector(showThoughtBubble) withObject:nil afterDelay:kBubbleDelay];
}

- (void)showThoughtBubble {
    thoughtBubble.hidden = FALSE;   
    [self startBubbleAnimations];
    [self performSelector:@selector(hideThoughtBubble) withObject:nil 

afterDelay:kAnimationDuration * kAnimationRepeatCount];
    }

- (void)hideThoughtBubble {
    bubble1.hidden = TRUE;
    bubble2.hidden = TRUE;
    bubble3.hidden = TRUE;
    thoughtBubble.hidden = TRUE;
    [bubbleAnimation stopAnimating];
    animating = FALSE;
}

I suppose I could create one large animation where the bubbles are all part of the same frame and each frame contains one more bubble, but I kind of wanted to be able to move the bubbles around a bit in Interface Builder and this approach doesn't offer much flexibility.

Any thoughts would be very much appreciated.

Thanks,

Michael

A: 

There are probably better ways to do this sequence, but one simple improvement would be to check to see if the view is visible before making the change. That way if the view has been replaced by another view, these will stop:

- (void)showBubble1 {
    if (bubble1.window) {   // check to see if view is visible
        bubble1.hidden = NO;
        [self performSelector:@selector(showBubble2) withObject:nil afterDelay:kBubbleDelay];
    }
}

Also, I'm not sure what thread you are starting this with, but make sure it is on the main thread. UI calls should only be done on the main thread. You can use one of the performSelectorOnMainThread: methods to start it off on the main thread.

progrmr