[英]How to prevent UIAnimation from interfering with CADisplayLink
在我當前的項目中,有兩個主要機制。 一系列在屏幕上連續移動的對象,以及一個機械師,您可以在其中按下多個按鈕以移動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];
}
}
我認為您將CADisplayLink用於所有運動機制。
但是,將移動邏輯委派給您的對象,而不是讓父對象管理所有移動。 這是您將在Unity中看到的模式。
在您的情況下,當CADisplayLink更新時,遍歷您的對象並在每個對象上調用update()方法。 此更新方法在正常狀態下將對象向上移動5點,就像BarMoving()所做的那樣。 在輕擊狀態下,它逐漸移至[241,37]。
當用戶輕擊對象時,將其狀態從法線更改為輕擊,設置上述初始速度,位置算法,目標點和update()方法將處理輕擊運動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.