我需要找到一个选项来延迟此对象(cloudDrop2图像)上的动画。当我加载游戏时,cloudDrop2 图像显示正在下降。我想在加载游戏后将动画延迟 5 秒。5秒后,图像显示下降。我正在使用 Xcode 5。
有什么帮助或建议吗?
这是我使用的代码:
在 .m 文件上
-(void)cloudDrop2Code{
// cloudDrop2 Images Animation
cloudDrop2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"cloudDrop2.png"],
[UIImage imageNamed:@"cloudDrop1.png"],
[UIImage imageNamed:@"cloudDrop2.png"],
[UIImage imageNamed:@"cloudDrop1.png"],
[UIImage imageNamed:@"cloudDrop2.png"],
[UIImage imageNamed:@"cloudDrop1.png"],
[UIImage imageNamed:@"cloudDrop2.png"],
[UIImage imageNamed:@"cloudDrop1.png"],nil];
[cloudDrop2 setAnimationRepeatCount:1];
cloudDrop2.animationDuration = 0.1;
[cloudDrop2 startAnimating];
// Drop Cloud Ramdom Position
cloudDrop2.center = CGPointMake(cloudDrop2.center.x, cloudDrop2.center.y +2.1);
if (cloudDrop2.center.y > 590){
RamdomPosition = arc4random() %266;
RamdomPosition = RamdomPosition +54;
cloudDrop2.center = CGPointMake(RamdomPosition, -40);
dropCloudUsed = NO;
// Time to Drop Cloud Down Again
[[NSRunLoop currentRunLoop]runUntilDate:[NSDate dateWithTimeIntervalSinceNow:7.0]];
}
}
然后在viewDidLoad
方法中:
// cloudDrop2 Down Movement Speed
TMcloudDrop2 = [NSTimer scheduledTimerWithTimeInterval:0.03
target:self
selector:@selector(cloudDrop2Code)
userInfo:nil
repeats:YES];
在单独的方法中创建计时器。
- (void)startGameAnimation
{
// cloudDrop2 Down Movement Speed
TMcloudDrop2 = [NSTimer scheduledTimerWithTimeInterval:0.03
target:self
selector:@selector(cloudDrop2Code)
userInfo:nil
repeats:YES];
}
然后在viewDidLoad
延迟调用此方法。
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:@selector(startGameAnimation) withObject:nil afterDelay:5];
}
欢迎来到 SO。
我没有看到任何负责加载游戏部分的代码。您可能应该发布它,以便更轻松地回答您的问题。
没有看到代码的任何其他部分,我建议您使用NSNotization在游戏完成加载时提醒您。收到通知后,您可以开始动画序列。
您可以阅读 NSNotification 类参考。如果您需要一些帮助或指示,请不要害怕询问。
要使用 5 秒的计时器延迟来启动动画,您可以使用以下内容:
[NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:@selector(targetMethod:)
userInfo:nil
repeats:NO];
其中(目标方法:)是包含动画的方法的名称。