今天animateWithDuration表現得非常愚蠢,我不知道爲什麼真的如此。我檢查了所有的東西,所有的括號和一切都很完美,但它始終呈現我沒有指定爲動畫的代碼行。animateWithDuration基於完全不相關的代碼行做動畫
好吧,所以這裏就是要點。我有一個longPressGestureRecognizer。它在一些UIImageView上,在longPress開始之後被拖動。當它在某個特殊區域結束時,我想啓動動畫,將UIImageView帶到其他位置並完成它。這裏是我的代碼:
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;
}
的情況是,一些奇怪的原因的代碼上面兩行得到的動畫,而不是真正的動畫,被指定。我通過在這些代碼行上添加註釋來檢查它 - 在這種情況下,動畫工作得很好。
有人可以請我清楚這一點,爲什麼會發生這種情況?這似乎是一個非常奇怪的行爲。
哪2行是你的意思? 原則上,應該在以下兩行執行動畫: self.frame = CGRectMake(newPlaceForCharacter.frame.origin.x-8,newPlaceForCharacter.frame.origin.y-5,40,55); self.alpha = 0.5; – 2012-07-15 21:16:38
我的意思是這兩個: TranslatePoint = CGPointMake(initialX,initialY); [[sender view] setCenter:translatedPoint]; – morgan1189 2012-07-15 21:17:42
我知道應該發生什麼,但它並不那麼奇怪。 – morgan1189 2012-07-15 21:17:59