1

次のコードを使用して、ラベルのテキストを点滅させています。

- (void)blinkAnimation:(NSString *)animationID finished:(BOOL)finished target:(UILabel *) target {

   NSString *selectedSpeed = [[NSUserDefaults standardUserDefaults] stringForKey:@"EffectSpeed"];
float speedFloat = (0.50 - [selectedSpeed floatValue]);

   [UIView beginAnimations:animationID context:(__bridge void *)(target)];
   [UIView setAnimationDuration:speedFloat];
   [UIView setAnimationDelegate:self];
   [UIView setAnimationDidStopSelector:@selector(blinkAnimation:finished:target:)];

   if([target alpha] == 1.0f)
       [target setAlpha:0.0f];
  else
       [target setAlpha:1.0f];

   [UIView commitAnimations];
}

次のコードを使用して、アニメーションを停止します。

- (void) stopAnimation{

   [self.gameStatus.layer removeAllAnimations];
}

アニメーションは正常に動作しますが、停止できません。

助けてくれませんか!

前もって感謝します....

4

1 に答える 1

4

問題はanimationDidStopSelector、アニメーションを手動で停止したときに your が呼び出されていることですが、そのメソッドは別のアニメーションを開始しているだけです。つまり、問題なく停止していますが、すぐに別のアニメーションを開始しています。

個人的にanimationDidStopSelectorは、アニメーションのオートリバース機能とリピート機能を削除して使用することをお勧めします。

[UIView beginAnimations:animationID context:nil];
[UIView setAnimationDuration:speedFloat];
[UIView setAnimationDelegate:self];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:CGFLOAT_MAX];
[target setAlpha:0.0f];
[UIView commitAnimations];

これで解決するはずですが、holex が言ったように、ブロック アニメーションを使用する必要があります。ドキュメントから引用します。「iOS 4.0 以降では、このメソッド [ beginAnimations] の使用は推奨されていません。代わりに、ブロックベースのアニメーション メソッドを使用してアニメーションを指定する必要があります。」

したがって、同等のブロック アニメーションは次のようになります。

[UIView animateWithDuration:1.0
                      delay:0.0
                    options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                 animations:^{
                     self.gameStatus.alpha = 0.0;
                 }
                 completion:nil];

どの手法を使用しても、removeAllAnimations期待どおりに機能するようになりました。

余談ですが、アニメーションを停止すると、alphaすぐにゼロに設定されます。アニメーションの繰り返しを停止してalphaから、現在の値から目的の最終値までアニメーション化する方が適切な場合があります。opacityこれを行うには、プレゼンテーション レイヤーから電流を取得し、alphaそこから停止したい場所にアニメーション化します (私の例では 1.0 ですが、0.0 も使用できます)。

CALayer *layer = self.gameStatus.layer.presentationLayer;
CGFloat currentOpacity = layer.opacity;

[self.gameStatus.layer removeAllAnimations];

self.gameStatus.alpha = currentOpacity;
[UIView animateWithDuration:0.25
                 animations:^{
                     self.gameStatus.alpha = 1.0;
                 }];
于 2013-02-16T21:01:43.120 に答える