喜 当某些事件发生时,我会调用一些方法(游戏中的解锁奖杯)它们会出现并消失一个子视图。 当这些事件同时发生并重叠时发生崩溃(正确)。 我如何确保“轮到你等待”? 感谢
-(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");
}
当这些事件同时发生并重叠时发生崩溃(正确)。 我如何确保“轮到你等待”? 感谢
答案 0 :(得分:1)
如果第二个动画在第一个动画完成之前开始,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:
部分传递给第三种方法。我希望代码是自我解释的。如果你没有得到这个,请告诉我。
下一点是关于这是否正确。你不能用阵列来管理吗?仅在显示屏中没有视图时才按下。