Xcode 如何在设置动画时检测两个图像帧的相交?

Xcode 如何在设置动画时检测两个图像帧的相交?,xcode,animation,uiview,uiimageview,intersect,Xcode,Animation,Uiview,Uiimageview,Intersect,在我的UIView中,我创建了一个名为targetView的UIImageView,并将其设置为一系列动画: [UIView animateWithDuration:1.0 animations:^{self.targetView.center = CGPointMake(220,20);} completion:^(BOOL finished){

在我的UIView中,我创建了一个名为targetView的UIImageView,并将其设置为一系列动画:

[UIView animateWithDuration:1.0
                 animations:^{self.targetView.center = CGPointMake(220,20);}
                 completion:^(BOOL finished){
                                             //another animation will begin;
                                            }];
我还创建了另一个名为shootView的UIImageView,它将在手指滑动时移动到目标方向。它的运动也作为动画实现。在动画结束时,检测与targetView的相交:

[UIView animateWithDuration:1.0
                 animations:^{
                 self.shootView.center = CGPointMake(destX, destY);}
                 completion:^(BOOL finished){
                            if (CGRectIntersectsRect(self.targetView.frame, self.shootView.frame)) {
                            //do something
                             }];

现在出现了一个问题:只有当targetView到达当前动画的终点并且shootView恰好在那里时,intersect命令才能正常工作。当TalkVIEW在动画中间某个地方移动时,即使在视觉上,也不会检测到相交,这两个帧相交非常明显。

< P>这种方法可能很棘手,因为你只能在UIVIEW动画的开始和结束时得到回调。在您的示例中,仅在动画完成后才调用CGRectIntersectRect方法

一种解决方案可能是使用NSTimer设置shootview位置的动画,并随着位置的每次更改进行碰撞检测。i、 e

[NSTimer timerWithTimeInterval: 0.03 target: self selector: @selector(timerTicked:) userInfo: nil repeats: YES];

-(void) timerTicked: (NSTimer*) timer {

  // do move
  // check for colisions
  // optional invalidation of timer
}

这种方法可能很棘手,因为您只能在UIView动画的开始和结束时获得回调。在您的示例中,仅在动画完成后才调用CGRectIntersectRect方法

一种解决方案可能是使用NSTimer设置shootview位置的动画,并随着位置的每次更改进行碰撞检测。i、 e

[NSTimer timerWithTimeInterval: 0.03 target: self selector: @selector(timerTicked:) userInfo: nil repeats: YES];

-(void) timerTicked: (NSTimer*) timer {

  // do move
  // check for colisions
  // optional invalidation of timer
}