2012-03-30 10 views
4

次のコードブロックを使用してUIViewをスライドさせ、別のUIViewを終了すると終了します。 アニメーションの2番目の部分では、完了ブロックは1回だけ実行されます。これは、最初のアニメーションが完了しなかったことを意味し、完了ブロックに到達しないことを意味します。 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

答えて

2

なぜあなたはanimateWithDurationブロックコード内の別のUIViewアニメーションを初期化しようとしていますか?コードを次のように更新して、一度に1つのビューの複数のアニメーションを実行していないことを確認してください。

- (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アニメーションを実行するための '古い'パラダイムを持つ新しいアニメーションルーチンを作成しています。 Appleは古いパラダイムから離れて人々を導いているので、ブロックだけを使用することをお勧めします。

これは、補完ブロックが1回だけ実行され、UIView animateWithブロックコードが1回だけ実行される理由です。ただし、内部アニメーションコードは複数回実行されます。

取り出し:

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

あなたのアニメーションブロックを複数回実行したい場合は、完全なメソッドを使用します。

  • animateWithDuration:遅延:オプション:アニメーション:完了:

遅延を0にして、オプションをUIViewAnimationOptionRepeatに設定するか、ブロックに必要なサイクル数を達成するために必要なものを設定します完全。ここで

は、あなたがそれを繰り返したいと仮定すると、私の提案です:

- (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; 
                  }]; 
          }]; 
} 
関連する問題