方法“等待轮到你”!

发布于 2024-11-09 05:55:56 字数 1186 浏览 0 评论 0原文

你好 当某些事件发生时,我调用许多方法(在游戏中解锁奖杯),它们确实会在子视图中出现和消失。 当这些事件同时发生并重叠时,就会发生崩溃(正确地)。 我如何确保“等待轮到你了:)”? 谢谢,

-(void)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");

}

当这些事件同时发生并重叠时,就会发生崩溃(正确地)。 我如何确保“等待轮到你了:)”? 谢谢

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

-(void)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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

獨角戲 2024-11-16 05:55:57

如果第二个动画在第一个动画完成之前开始,TrofeoView 将被重新分配,并且您将丢失之前的引用。当您在 'trofeiLaunch:` 方法中释放 TrofeoView 时,您也违反了内存管理规则。稍后您在获得所有权后再次使用该变量。仅当您已完成但尚未完成的对象时才应释放该对象。

我认为沿着这条路走下去最重要的是确保三个方法引用正确的对象。默认情况下,第一个是可以的,因此您需要处理接下来的两个。我已经修改了你的代码来做到这一点。

- (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];
}

我通过 context 将视图传递给第二个方法,将 withObject: 部分传递给第三个方法。我希望代码是不言自明的。如果您不明白,请告诉我。

下一点是关于这是否是正确的方法。你不能使用数组来管理它吗?仅当显示中没有视图时才推送。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文