我正在使用iPhone应用程序,在我的应用程序中,有一个对象从屏幕的顶部移动到底部。为此,我使用CADisplay链接。一旦对象离开屏幕,它应该重新开始它的路线。我遇到的问题是,每次对象重新启动其路径时,它都会加速。这一直持续到物体进入如此之快以至于你几乎看不到它为止。任何想法为什么会发生这种情况以及如何阻止它?感谢任何帮助,提前谢谢!
-(void)spawnButton{
int x = (arc4random() % (240) + 40;
int y = -100;
button1.center = CGPointMake(x,y);
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(moveObject)];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
-(void) moveObject {
int z = 1;
button1.center = CGPointMake(button1.center.x , button1.center.y +z);
if (button1.center.y >= 480) {
[self spawnButton];
}
}
答案 0 :(得分:1)
您正在为spawnButton
的每次调用创建新的显示链接。如果您没有做任何事情来从运行循环中删除旧的显示链接,那么旧的显示链接将继续发送moveObject:
消息。因此,在两次拨打spawnButton
后,每个视频帧会收到两条moveObject:
条消息,三次通话后,每个视频帧会收到三条moveObject:
条消息,依此类推。
答案 1 :(得分:0)
我似乎通过每次重新启动对象路径时使显示链接无效来解决问题。
if (button1.center.y >= 480) {
[self spawnButton];
[displaylink invalidate];
}
}