2012-07-15 23 views
0

今天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; 
} 

的情況是,一些奇怪的原因的代碼上面兩行得到的動畫,而不是真正的動畫,被指定。我通過在這些代碼行上添加註釋來檢查它 - 在這種情況下,動畫工作得很好。

有人可以請我清楚這一點,爲什麼會發生這種情況?這似乎是一個非常奇怪的行爲。

+0

哪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

+0

我的意思是這兩個: TranslatePoint = CGPointMake(initialX,initialY); [[sender view] setCenter:translatedPoint]; – morgan1189 2012-07-15 21:17:42

+0

我知道應該發生什麼,但它並不那麼奇怪。 – morgan1189 2012-07-15 21:17:59

回答

1

如果您將return;從完成塊(它沒有做任何事情)移動到動畫設置後,它應該工作。

[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:^{ 
    NSLog(@"animation completed"); 
    self.alpha=1; 
    //code referring to the finalizing of moving, doesn't matter here 
}]; 
return; 
+0

那麼,雖然我仍然無法理解這一點,但爲什麼這個回報非常重要?你能向我解釋一下嗎? – morgan1189 2012-07-16 05:55:16

+0

你的代碼沒有返回到最後兩行之前,因爲房產中心是動畫的,你看到了。通過在完成塊外移動返回值,您的方法調用就會在state = ended的情況下結束您期望的執行結束。 – tobinjim 2012-07-17 01:59:45

+0

非常感謝。 – morgan1189 2012-07-17 08:29:04

相關問題