UIImageView无法正确响应center.x.

时间:2012-02-09 18:37:37

标签: iphone objective-c uiimageview

我正试图在窗口周围放一个图像(当它从左边掉下来时,它再次出现在右边)我有一个定时器调用一个包含这个的函数:

bgImage.center = CGPointMake(bgImage.center.x - 5, bgImage.center.y);
if (bgImage.center.x < -self.view.frame.size.width/2)
    bgImage.center = CGPointMake(bgImage.center.x + self.view.frame.size.width, bgImage.center.y);
    NSLog(@"%f",bgImage.center.x);

当它从屏幕上掉下来时,它会回到中途,然后每次都会少一点。控制台为x值返回非常大的值

2012-02-09 13:33:44.229 Parkour Runner[1351:f803] 2: 28990.000000
2012-02-09 13:33:44.328 Parkour Runner[1351:f803] 2: 29285.000000
2012-02-09 13:33:44.429 Parkour Runner[1351:f803] 2: 29580.000000
2012-02-09 13:33:44.528 Parkour Runner[1351:f803] 2: 29875.000000

1 个答案:

答案 0 :(得分:1)

相反怎么样?它将考虑图像的大小。

bgImage.frame = CGRectMake(bgImage.origin.x - 5.0f, bgImage.origin.y, bgImage.size.width, bgImage.size.height);

if ( (bgImage.frame.origin.x + bgImage.frame.size.width) < 0.0f) {
    bgImage.frame = CGRectMake(self.view.frame.size.width, bgImage.origin.y, bgImage.size.width, bgImage.size.height);
}