2013-07-18 39 views
0

使用此代码来尝试检测两个图像之间的冲突,其中一个图像处于动画中,但不起作用。使用UIAnimation检测冲突

[UIView animateWithDuration:5 animations:^{ 
     bird.center = CGPointMake(bird.center.x, 600); 
     fallTimer = [NSTimer scheduledTimerWithTimeInterval:.001 target:self selector:@selector(check) userInfo:nil repeats:YES]; 
    }]; 

-(void)check { 
    if (CGRectIntersectsRect(bird.frame, cat.frame)) { 
     NSLog(@"YES"); 
    } 
} 

如何检测碰撞?

+0

你的时间间隔是可笑的小(0.001秒)。它在代码中看起来像鸟的位置是静态的。猫是否在移动,你可以在猫动画中进行检查吗? –

+0

猫是动态的,而猫是静态的。 –

回答

0

在动画中不能使用视图的框架,返回的值将不准确。相反,您应该能够从layer的视图中获取presentationLayer并检查其框架。

+0

我该怎么做? –

+0

而不是'bird.frame',请尝试'bird.layer.presentationLayer.frame'。 – Wain

+0

尝试过它,得到以下错误:在'id'类型的对象上找不到'属性'框架。任何其他想法? –