如何修复animateWithDuration在遇到CGRectIntersectsRect时停止它们?

时间:2015-01-24 22:45:16

标签: ios objective-c

我有一个动画的图像(在屏幕上显示然后返回到屏幕上),但我遇到的问题是当animateWithDuration遇到CGRectIntersectsRect然后CGRectIntersectsRect被解雇时。两张图片之间没有勾结。我希望动画的图像与CGRectIntersectsRect发生碰撞。然后CGRectIntersectsRect被解雇了。换句话说,我遇到的问题是图像可能会高于CGRectIntersectsRect图像,但CGRectIntersectsRect仍会被触发。两张图片之间没有勾结。我想解决这个问题,这样当两个图像碰撞时 CGRectIntersectsRect代码在y-xis上遇到时会被触发。

继承我的代码:

 NSTimeInterval durationUp = 0.5;
[UIView animateWithDuration:durationUp delay:0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     CGRect f = jumperguy.frame;
                     f.origin.y -= 150;
                     jumperguy.frame = f;



                 }
                 completion:nil];

[UIView animateWithDuration:0.5  delay:durationUp
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^{
                     CGRect f = jumperguy.frame;
                     f.origin.y += 150;
                     jumperguy.frame = f;



                 }
                 completion:nil];

if (CGRectIntersectsRect(jumperguy.frame, hurdle2.frame)) {

    [movement invalidate];

    l.hidden = NO;

}

1 个答案:

答案 0 :(得分:0)

你要做的事情是行不通的。你需要一种完全不同的方法。

当您使用Core Animation或UIView动画时,对象会立即移动到它的最终位置。它只是动画的基础“表示层”。

为了做你想做的事情,你需要一个计时器来触发检查每一帧的动画。您可能希望将CADisplayLink用于计时器,因为它与NSTimer不同,因为它快速且帧精确。然后,在计时器代码中,您必须检查正在设置动画的图层的表示层。

UIView动画通常会为正在设置动画的视图层设置动画,因此您可以使用以下代码检查视图图层的表示层:

CGRectIntersectsRect(jumperguy.layer.presentationLayer.frame,hurdle2.layer.presentationLayer.frame)

我没有尝试做你想做的事情,所以我不肯定它会起作用,但它应该。