Question

I am using a UIView animation to move a button across the screen. While it is moving, I have an NSTimer running the method

checkForCollision

which looks like this:

for(UIButton* b in squares)
    if(CGRectIntersectsRect(playerSquare.frame, b.frame)) {
        [self showEndMenu];
        break;
    }

with an interval of .05; I would like for that method to notify me any time the button moves across the path of another. The problem, it appears, is that when it checks for the frame of the UIButton b, it only sees the frame towards which the button is moving.

I have the button being animated like this:

[UIView beginAnimations:@"" context:nil];
[UIView setAnimationDuration:1.3];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
view.center = destPoint;
[UIView commitAnimations];

So, if the destPoint intersects the CGRect playerSquare.frame, [self showEndMenu] is called. But if the frame / position at which the button is, say, halfway through the animation, intersects playerSquare, [self showEndMenu] doesn't get called. I don't know how to solve this; I can provide more code if need be. Thank you!

Was it helpful?

Solution

Moving views by animating their properties immediately sets the property to the final value, as you have found.

You _may_be able to get the intermediate values out using the core animation layers (see http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Animation_Types_Timing/Articles/PropertyAnimations.html#//apple_ref/doc/uid/TP40006672-SW1 typing on phone sorry for poor link) but honestly I've not got any experience with that. If anyone does it would be great to see it here. In fact, a quick search has turned up this question, with the exact answer you are looking for, I think :)

UIView animation determine center during animation

Your other option is to move the button iteratively using timers, in the same way that objects are moved around using cocos2d, so when you are checking for position each time then your buttons are where they really are. However this way you have to calculate the path yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top