Today animateWithDuration was acting really stupid and I don’t know why really. I checked all the things, all the brackets and everything was perfect, but it was always presenting the line of code i didn’t specify as an animation.
Ok, so here’s the point. I have a longPressGestureRecognizer. It’s on some UIImageView, which is being dragged, after the longPress begins. When it ends in some special zone I want to start the animation, taking the UIImageView to some other place and finish it. Here’s my code:
if ([(UILongPressGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGPoint dropPoint = CGPointMake(self.center.x, self.center.y-5);
for (UIView * placeForCharacter in delegate.subviews) {
if ([placeForCharacter isKindOfClass:[PlaceForCharacterCircle class]]) {
PlaceForCharacterCircle * newPlaceForCharacter = (PlaceForCharacterCircle *) placeForCharacter;
if (CGRectContainsPoint(newPlaceForCharacter.dropZone, dropPoint)) {
UIViewAnimationOptions options = 0;
[UIView animateWithDuration:0.2 delay:0.0 options:0 animations:^{
self.frame = CGRectMake(newPlaceForCharacter.frame.origin.x-8, newPlaceForCharacter.frame.origin.y-5, 40, 55);
self.alpha = 0.5;
}
completion:^(BOOL finished){
NSLog(@"animation completed");
self.alpha=1;
//code referring to the finalizing of moving, doesn't matter here
return;
}];
} else {
if (CGRectContainsPoint(newPlaceForCharacter.dropZone, dropPoint)) {
//code referring to the moving to the other place in case the UIImageView was dropped on another zone that is full already, doesn't matter here
return;
}
}
}
}
//code referring to the comeback of a UIImageView in case it was dropped somewhere else, doesn't matter here
//These two are the lines that get executed when the animation starts
//THEY BEGIN
translatedPoint = CGPointMake(initialX, initialY);
[[sender view] setCenter:translatedPoint];
//THEY END
return;
}
The case is that for some strange reason the two lines of code above get animated instead of the REAL animation, that was specified. I double-checked it by putting the comments on these lines of code – in this case the animation works just fine.
Could someone please clear this out for me, why does this happen? It seems to be a really strange behaviour.
If you move the
return;from the completion block (where it’s not doing anything) to after the animation is setup it should work.