2012-03-30 52 views
4

我使用下面的代碼塊向下滑動UIView,並在完成時旋轉另一個UIView。 動畫的第二部分,完成塊只執行一次,這意味着第一個動畫未完成,否則它將到達完成塊。 在iphone模擬器上看起來好像第一個動畫沒有完成... 任何人都可以幫我弄清楚這一點嗎? 我的NSLog說:我的animatewithduration,完成塊只執行一次

完成第一次

開始第二

完成第一次

完成第一次

完成1


- (IBAction) move 
{ 
[UIView animateWithDuration:0.7 animations:^{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.7]; 
    [UIView setAnimationRepeatCount:1]; 
    [UIView setAnimationRepeatAutoreverses:NO]; 

    CGPoint pos = movingtTable.center; 
    float moveDistance = 220.0; 
    if(!isViewVisible){ 
     //expose the view 
     pos.y = pos.y+moveDistance; 
     //disable selection for xy table 
     xTable.userInteractionEnabled = NO; 
     yTable.userInteractionEnabled = NO; 
     //angle = M_PI; 
    } 
    else 
    { 
     pos.y = pos.y-moveDistance; 
     xTable.userInteractionEnabled = YES; 
     yTable.userInteractionEnabled = YES; 
     //angle = -M_PI; 
    } 
    isViewVisible = !isViewVisible; 
    movingtTable.center = pos;  
    NSLog(@"finished 1st"); 


}completion:^(BOOL finished){ 
    NSLog(@"started 2nd"); 
     [UIView animateWithDuration:0.4 animations:^{ 
      //[UIView beginAnimations:nil context:NULL]; 
      [UIView setAnimationDuration:0.4]; 
      //[UIView setAnimationRepeatCount:1]; 
      //[UIView setAnimationRepeatAutoreverses:NO]; 
      arrows.transform = CGAffineTransformMakeRotation(angle); 
     }completion:^(BOOL finished){ 
      angle = -angle; 
     }]; 
}]; 
+0

丹,要麼在回答對您有幫助?你有沒有發現他們工作?如果是這樣,如果你「接受」最好的或者對你的發現發表評論,對每個人都有幫助。乾杯 – MobileVet 2012-04-02 14:51:32

回答

2

爲什麼你想初始化animateWithDuration塊碼內的另一個UIView的動畫?將代碼更新爲以下內容,並確保您一次不會執行單個視圖的多個動畫。

- (IBAction) move 
{ 
[UIView animateWithDuration:0.7 animations:^{ 
    CGPoint pos = movingtTable.center; 
    float moveDistance = 220.0; 
    if(!isViewVisible){ 
     //expose the view 
     pos.y = pos.y+moveDistance; 
     //disable selection for xy table 
     xTable.userInteractionEnabled = NO; 
     yTable.userInteractionEnabled = NO; 
     //angle = M_PI; 
    } 
    else 
    { 
     pos.y = pos.y-moveDistance; 
     xTable.userInteractionEnabled = YES; 
     yTable.userInteractionEnabled = YES; 
     //angle = -M_PI; 
    } 
    isViewVisible = !isViewVisible; 
    movingtTable.center = pos;  
    NSLog(@"finished 1st"); 
} 
completion:^(BOOL finished){ 
    NSLog(@"started 2nd"); 
     [UIView animateWithDuration:0.4 animations:^{ 
      arrows.transform = CGAffineTransformMakeRotation(angle); 
     }completion:^(BOOL finished){ 
      angle = -angle; 
     }]; 
}]; 

BTW:該塊代碼需要一些嚴重的重構,如果你問我:)

2

你是混合和匹配的範例,我相信是造成你所看到的問題。您正在創建一個動畫塊,但在該塊的內部,您將創建一個新的動畫例程,其中包含用於運行UIView動畫的「舊」範例。蘋果正在引導人們遠離舊模式,我鼓勵你只使用模塊。

這就是爲什麼完成塊只運行一次,UIView animateWith塊代碼只運行一次。但是,您的內部動畫代碼會多次運行。

取出:

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.7]; 
[UIView setAnimationRepeatCount:1]; 
[UIView setAnimationRepeatAutoreverses:NO]; 

如果你希望你的動畫塊跑幾次,然後用完整的方法:

  • animateWithDuration:延遲:選項:動畫:完成:

使延遲= 0,並將您的選項設置爲UIViewAnimationOptionRepeat,或者任何您需要完成的循環次數您想要塊t完整。

這裏是我的建議,假設你希望它重複:

- (IBAction) move 
{ 
[UIView animateWithDuration:0.7 
         delay:0 
        options:UIViewAnimationOptionRepeat 
       animations:^{ 
           CGPoint pos = movingtTable.center; 
           float moveDistance = 220.0; 

           if(!isViewVisible) { 
           //expose the view 
           pos.y = pos.y+moveDistance; 
           //disable selection for xy table 
           xTable.userInteractionEnabled = NO; 
           yTable.userInteractionEnabled = NO; 
           //angle = M_PI; 
           } 
           else { 
           pos.y = pos.y-moveDistance; 
           xTable.userInteractionEnabled = YES; 
           yTable.userInteractionEnabled = YES; 
           //angle = -M_PI; 
           } 
           isViewVisible = !isViewVisible; 
           movingtTable.center = pos;  
           NSLog(@"finished 1st"); 
          } 
       completion:^(BOOL finished){ 
           NSLog(@"started 2nd"); 
           [UIView animateWithDuration:0.4 
               animations:^{ 
                   arrows.transform = CGAffineTransformMakeRotation(angle); 
                  } 
               completion:^(BOOL finished){ 
                   angle = -angle; 
                  }]; 
          }]; 
}