首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止UIAnimation对CADisplayLink的干扰

如何防止UIAnimation对CADisplayLink的干扰
EN

Stack Overflow用户
提问于 2015-01-13 22:49:00
回答 1查看 134关注 0票数 1

在我目前的项目中,有两个主要的机制。一系列不断向屏幕上方移动的对象,以及按多个按钮来移动2个对象的机械师。对于在屏幕上移动的一系列对象,我使用了CADisplayLink,而另一个机制是使用UIAnimation,但是当我运行项目时,我注意到UIAnimation会在按下按钮时干扰与CADisplayLink链接的对象的移动。我该如何纠正这个问题?

下面是我为这两位机械师编写的代码

代码语言:javascript
复制
-(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];

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-13 23:34:04

我相信你对所有的运动机制都使用了CADisplayLink。

但是,将移动逻辑委托给您的对象,而不是有一个管理所有移动的父对象。这就是你们将在团结中看到的模式。

在您的示例中,当CADisplayLink更新时,循环遍历对象并对每个对象调用update()方法。此更新方法在正常状态下将对象向上移动5点,如BarMoving()所做的。在抽头状态下,它逐渐移动到241,37。

当用户点击对象时,将其状态从普通状态更改为抽头状态,设置上面提到的初始速度、位置算法、目标点和update()方法将处理点击运动。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27932938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档