0

以下に示すように、ラベルをフェードインおよびフェードアウトする非常に単純な繰り返しアニメーションを実行しています。アニメーションが終了するたびに完了ブロックが呼び出されると想定しましたが、それを使用UIViewAnimationOptionRepeatすると呼び出されることはありません。では、このアニメーションを停止するにはどうすればよいでしょうか。

を使用できることはわかっていますが[self.lbl.layer removeAllAnimations];、それは非常に突然終了します。アニメーションのサイクルがいつ終了したかを知りたいので、その時点で停止できます。

[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut animations:^{
            self.lbl.alpha = 0;

        } completion:^(BOOL finished){
            if (finished) NSLog(@"done");

        }];
4

3 に答える 3

3

UIViewAnimationOptionRepeatオプションでアニメーションを有限にしたい場合は、UIView の+ (void)setAnimationRepeatCount:(float)repeatCountで繰り返し回数を設定する必要があります。

ブロックベースのアニメーションの場合 (あなたの場合)、アニメーション ブロック内から繰り返し回数を設定する必要があります。したがって、変更されたコードは次のとおりです。

[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut animations:^{
        [UIView setAnimationRepeatCount:4]; // 4 as example here
        self.lbl.alpha = 0;

    } completion:^(BOOL finished){
        if (finished) NSLog(@"done");

    }];

これはsetAnimationRepeatCount:、UIView のメソッドのドキュメントに記載されています。

于 2015-08-27T21:19:07.797 に答える
1

おそらく、セレクターを使用したこの種のソリューションが役立ちます。

- (void) animateTextWithMax:(NSNumber *)max current:(NSNumber *)current
{
    NSLog(@"max = %d, current = %d",max.intValue, current.intValue);
    textlabel.alpha = 1.0f;
    [UIView animateWithDuration:1.0f delay:0 options:UIViewAnimationOptionAutoreverse
                     animations:^{
                         textlabel.alpha = 0.0f;
                     }completion:^(BOOL finished){
                         NSLog(@"finished");
                         if (current.intValue < max.intValue) {
                             [self performSelector:@selector(animateTextWithMax:current:) withObject:max withObject:[NSNumber numberWithInteger:(current.intValue+1)]];
                         }

                     }];
}

次に、次の方法でアニメーションを呼び出すことができます。

[self animateTextWithMax:[NSNumber numberWithInt:3] current:[NSNumber numberWithInt:0]];

オプションを使用していないため、これは最善の解決策ではないかもしれませんが、うまくいくUIViewAnimationOptionRepeatと思います。

これがお役に立てば幸いです。

于 2013-10-20T17:09:59.670 に答える
0

お気づきのように、アニメーションは決して終了しないため、完了ブロックは呼び出されません。私の頭の上には、2 つのオプションがあります。

  • オプションを使用して、別のアニメーションをラベルに追加し、目的の最終値に設定しUIViewAnimationOptionBeginFromCurrentStateます。ただし、これが既存の繰り返しアニメーションにどのような影響を与えるかはわかりません。それがうまくいかない場合...
  • を調べて、アニメーションの現在の位置を取得しますlabel.layer.presentationLayer。すべてのアニメーションを停止し、値を現在の状態に設定してから、現在の状態から目的の最終状態に遷移する新しいアニメーションを追加します。
于 2013-10-20T17:00:28.083 に答える