3

アプリに挿入する小さなローダー アニメーションを作成したいと考えていました。以前は CGAnimations で問題なくアニメーションを繰り返していましたが、今回はブロック アプローチを使用しました。

私は小さなテストを行っていますが、次のコードを繰り返すことができます:

- (void) startLoading {

    __block int count = 0;

    [UIView animateWithDuration:0.4
                          delay: 0.0
                        options: UIViewAnimationOptionRepeat
                     animations:^{
                         count++;
                     }
                     completion:^(BOOL finished){

                         if (count > 5)
                             count = 0;
                         NSLog(@"%d", count);

                     }];

}

- (void) stopLoading {

}

上記は完了ブロックを 1 回だけ起動し、繰り返されません。

カウントが増えるようにブロックを繰り返すにはどうすればよいですか?

これを機能させてアニメーションを繰り返しブロックに入れると、 stopLoading には何が入りますか: アニメーションを再び停止するには?

助けてくれてありがとう:)

4

1 に答える 1

5

以下は、有限の繰り返しアニメーションです。

- (void) animate: (int) count {
    CGPoint origC = v.center;
    void (^anim) (void) = ^{
        v.center = CGPointMake(100,100);
    };    
    void (^after) (BOOL) = ^(BOOL finished) {
        v.center = origC;
        if (count)
            [self animate:count-1];
    };
    int opts = UIViewAnimationOptionAutoreverse;
    [UIView animateWithDuration:1 delay:0 options:opts 
                     animations:anim completion:after];
}

ここには再帰があるので、行き過ぎたり、メモリが不足したりしたくありませんが、カウントを制限すれば (あなたの例では 5 でした)、うまくいくはずです。

于 2011-12-06T03:33:54.680 に答える