In my UIView, I’ve created a UIImageView called targetView where I have set it to a series of animations:
[UIView animateWithDuration:1.0
animations:^{self.targetView.center = CGPointMake(220,20);}
completion:^(BOOL finished){
//another animation will begin;
}];
I also created another UIImageView called shootView which will move to the target direction upon a finger swipe. Its movement is also implemented as an animation. At the end of its animation, detect the intersect with the 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
}];
Now there is a problem: the intersect command only works fine if the targetView has reached the end point of the current animation and the shootView happens to be there. While the targetView is on the move somewhere in the middle of an animation, no intersect can be detected even if visually it is very obvious that the two frames are intersecting.
This approach might be tricky since you can only get callbacks on the start and end of UIView animations. In your example the CGRectIntersectRect method is only being called after the animation has completed.
One solution may be to use an NSTimer to animate the position of the shootview, and with each change of position do the collision detection. i.e