开发者

method "wait your turn"!

开发者 https://www.devze.com 2023-03-07 06:16 出处:网络
hi when certain events occur, I call a number of methods (unlocked trophies in a game) they do appear and disappear a sub-view.

hi when certain events occur, I call a number of methods (unlocked trophies in a game) they do appear and disappear a sub-view. Crash (rightly) occurs when these events occur simultaneously and overlap. How do I make sure that " wait your turn :) "? thanks

-(voi开发者_运维百科d)trofeiLaunch:(int)x {


    CGRect loseFrame = CGRectMake(0, 0, 480, 320);
    TrofeoView = [[UIView alloc] initWithFrame:loseFrame];
    TrofeoView.alpha = 0.0;
    [UIView beginAnimations:nil context:self];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:0.5];
    [UIImageView setAnimationDelegate:self];
    TrofeoView.alpha = 1.0;
    [UIView setAnimationDidStopSelector:@selector(bridgeTrofei)];
    [UIView commitAnimations];
    [self.view addSubview:TrofeoView];
    [TrofeoView release];

            ....

}

-(void)bridgeTrofei {

TrofeoView.alpha = 1.0;
[UIView beginAnimations:nil context:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
TrofeoView.alpha = 0.0;
[UIView commitAnimations];
[self performSelector:@selector(deleteTrofei) withObject:self afterDelay:1.0];

}


-(void)deleteTrofei {


[TrofeoView removeFromSuperview];

NSLog(@"delete");

}

Crash (rightly) occurs when these events occur simultaneously and overlap. How do I make sure that " wait your turn :) "? thanks


If the second animation starts before the first animation is complete, TrofeoView will get reassigned and you will lose the earlier reference. You also flout memory management rules when you release TrofeoView in 'trofeiLaunch:` method. You use this variable again later with having taken ownership. You should only release an object if you are done with it which you are not.

I think the most important thing going down this path is to make sure that three methods refer to the correct object. First one is ok by default so you need to handle the next two. I have modified your code to do that.

- (IBAction)start {

    animatingView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    animatingView.backgroundColor = [UIColor greenColor];
    animatingView.alpha = 0.0;
    [UIView beginAnimations:@"Trophy Display" context:animatingView];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:0.5];
    [UIImageView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    animatingView.alpha = 1.0;
    [UIView commitAnimations];
    [self.window addSubview:animatingView];
}

- (void)animationDidStop:(NSString*)animationID finished:(NSNumber*)finished context:(void *)context {

    if ( [animationID isEqualToString:@"Trophy Display"] ) {
        UIView *aView = (UIView *)context;

        [UIView beginAnimations:nil context:aView];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelegate:self];
        aView.alpha = 0.0;
        [UIView commitAnimations];
        [self performSelector:@selector(finishAnimation:) withObject:aView afterDelay:1.0];
    }
}

- (void)finishAnimation:(UIView*)aView {
    [aView removeFromSuperview];
    [aView release];
}

I pass the view via context to the second method and withObject: part to the third method. I am hoping the code is self explanatory. Let me know if you don't get this.

Next point is about whether this is the right way. Can't you manage this using an array? Push only if there is no view in display.

0

精彩评论

暂无评论...
验证码 换一张
取 消