【问题标题】:Looping moving background objective-C循环移动背景物镜-C
【发布时间】:2011-10-02 01:10:03
【问题描述】:

我正在测试一个背景循环动画,其中将有两个尺寸为 1024x768 像素的图像,向左移动,离开屏幕,然后跳回另一侧,然后重复。

我可以通过为两个背景图像创建一个恒定的速度移动来做到这一点(成功),然后我尝试了下面的代码让它跳转,但是出现了一个问题:

    if((background.center.x) < -511){
        background.center = CGPointMake(1536, background.center.y);
    }

    if((background2.center.x) < -511){
        background2.center = CGPointMake(1536, background2.center.y);
    }

不知何故,这并没有按我预期的方式工作。每次都会留下几个像素的间隙,我很困惑为什么。有谁知道导致这种情况发生的原因以及如何解决?谢谢!

【问题讨论】:

  • iOS,恒速变量为10

标签: objective-c cocoa-touch animation


【解决方案1】:

您似乎忘记考虑移动的距离。大于表达式可能已被触发,因为您移动到很远。我猜你的运动大于 1 像素/帧。

我不确定什么样的价值观会影响你的运动,但我认为考虑到你应该做的运动......

 if ((background.center.x) < -511){
    CGFloat dist = background.center.x + 512;
    background.center = CGPointMake(1536+dist, background.center.y);
 }

 if ((background2.center.x) < -511){
    CGFloat dist = background2.center.x + 512;
    background2.center = CGPointMake(1536+dist, background2.center.y);
 } 

【讨论】:

    【解决方案2】:

    我不会让两个图像独立移动(某种程度),而是跟踪单个backgroundPosition 变量,然后不断更新两个图像相对于那个位置的位置。这应该保持一切整洁:

    CGFloat const backgroundWidth = 1024;
    CGFloat const backgroundSpeed = 2;
    
    - (void)animateBackground {
        backgroundPosition -= backgroundSpeed;
        if (backgroundPosition < 0) {
            backgroundPosition += backgroundWidth;
        }
        background1.center.x = backgroundPosition - backgroundWidth/2;
        background2.center.x = backgroundPosition + backgroundWidth/2;
    }
    

    【讨论】:

    • 感谢您的回答,我根据您的回答找到了不同的解决方案...我发现这个问题是由每帧像素的位移引起的,因为我的常数是 10,我从中减去了 10 512,也就是1024的一半,所以在碰到另一边之前会移动到另一边10px,因为那10px会在下一帧之后消失,这样就解决了问题。非常感谢! :)
    • 我们在这里提供解决方案。很高兴听到你成功了! :)
    猜你喜欢
    • 2015-08-01
    • 1970-01-01
    • 2021-04-10
    • 2017-11-22
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    相关资源
    最近更新 更多