I’m working on an app where two views meet somewhere on the screen. When they meet, a collision detector fires off this method. It’s supposed to identify the appropriate base and then send both views to it. It does exactly that, but it happens instantly instead of over a period of 4 seconds. What am I missing? RADIUS is defined above this code. Does it have anything to do with arrow not being a UIView? The spriteView class is a subclass of UIView.
-(void)sendToBase:(spriteView *)arrow
{
int teamNumber = arrow.teamNumber;
// Find the location of the base.
for (UIView *scaledView in self.view.subviews) {
if (scaledView.tag == 100) {
for (UIView *base in scaledView.subviews) {
if (base.tag >= 1000) {
if (teamNumber + 1000 == base.tag) {
// We found the right base.
CGPoint newCenter;
newCenter.x = base.center.x + arc4random() % (int) floor(RADIUS) - RADIUS/2.0;
newCenter.y = base.center.y + arc4random() % (int) floor(RADIUS) - RADIUS/2.0;
[UIView animateWithDuration:4.0 delay:0.0 options:UIViewAnimationCurveLinear animations:^{
[arrow setCenter:newCenter];
} completion:^(BOOL finished) {
// After walking back to base, remove and create new objects
[arrow removeFromSuperview];
[self addArrow:scaledView toTeam:teamNumber];
}];
}
}
}
}
}
}
On the off-chance that the class type mismatch was the problem, I modified the code thusly, with the same results.
-(void)sendToBase:(spriteView *)arrow
{
UIView *uiSpriteView = (UIView *)arrow;
int teamNumber = arrow.teamNumber;
// Find the location of the base.
for (UIView *scaledView in self.view.subviews) {
if (scaledView.tag == 100) {
for (UIView *base in scaledView.subviews) {
if (base.tag >= 1000) {
if (teamNumber + 1000 == base.tag) {
// We found the right base.
CGPoint newCenter;
newCenter.x = base.center.x + arc4random() % (int) floor(RADIUS) - RADIUS/2.0;
newCenter.y = base.center.y + arc4random() % (int) floor(RADIUS) - RADIUS/2.0;
[UIView animateWithDuration:4.0 delay:0.0 options:UIViewAnimationCurveLinear animations:^{
[uiSpriteView setCenter:newCenter];
} completion:^(BOOL finished) {
// After walking back to base, remove and create new objects
[arrow removeFromSuperview];
[self addArrow:scaledView toTeam:teamNumber];
}];
}
}
}
}
}
}
I solved the problem (using setCenter). There are two animations going on.
The first animation occurs before the collision.
The second animation occurs as a result of the collision.
The code that detects the collision (and thus spawns the second animation) was in the animation block instead of the completion block. When I moved the code to the completion block, the second animation worked. Well, almost. One of the two arrows were animated properly. I can probably ferret out the other issue now that one of them is working.