在我当前的项目中,有两个主要机制。一系列不断向上移动屏幕的对象,以及按下多个按钮移动 2 个对象的机制。对于在屏幕上移动的一系列对象,我使用了 CADisplayLink,而另一个机制是使用 UIAnimation,但是当我运行我的项目时,我注意到 UIAnimation 会干扰与链接的对象的移动每当按下按钮时,CADisplayLink 会持续一瞬间。我该如何纠正这个问题?
以下是我对这两种机制的代码
-(IBAction)tap{
CGRect frame = Person.frame;
frame.origin.x = 16;
frame.origin.y = 37;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
Person.frame = frame;
[UIView commitAnimations];
}
-(IBAction)tap1{
CGRect frame = Person.frame;
frame.origin.x = 241;
frame.origin.y = 37;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
Person.frame = frame;
[UIView commitAnimations];
}
-(IBAction)tapR1{
CGRect frame = Person1.frame;
frame.origin.x = 302;
frame.origin.y = 37;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
Person1.frame = frame;
[UIView commitAnimations];
}
-(IBAction)tapR2{
CGRect frame = Person1.frame;
frame.origin.x = 526;
frame.origin.y = 37;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
Person1.frame = frame;
[UIView commitAnimations];
}
-(void)GameOver{
}
-(void)Score{
ScoreNumber = ScoreNumber + 1;
ScoreLabel.text = [NSString stringWithFormat:@"%i", ScoreNumber];
}
-(IBAction)StartGame:(id)sender{
StartGame.hidden = YES;
Spike.hidden = NO;
Spike1.hidden = NO;
Spike2.hidden = YES;
SpikeR.hidden = NO;
SpikeR1.hidden = NO;
SpikeR2.hidden = NO;
circle.hidden = NO;
ScoreLabel.hidden = NO;
[self PlaceBars];
[self PlaceBars1];
Movement = [CADisplayLink displayLinkWithTarget:self selector:@selector(BarMoving)];
[Movement addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
-(void)BarMoving{
Spike.center = CGPointMake(Spike.center.x, Spike.center.y - 5);
Spike1.center = CGPointMake(Spike1.center.x, Spike1.center.y - 5);
Spike2.center = CGPointMake(Spike1.center.x, Spike1.center.y - 5);
if (Spike2.center.y == -115) {
[self PlaceBars];
}
SpikeR.center = CGPointMake(SpikeR.center.x, SpikeR.center.y - 5);
SpikeR1.center = CGPointMake(SpikeR1.center.x, SpikeR1.center.y - 5);
SpikeR2.center = CGPointMake(SpikeR2.center.x, SpikeR2.center.y - 5);
if (SpikeR2.center.y == -115) {
[self PlaceBars1];
}
}