重复 UIAnimation 块,以及再次停止它的方法

发布于 2024-11-19 00:10:38 字数 796 浏览 7 评论 0原文

我想做一个小的加载器动画来放入我的应用程序中。我之前用 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 {

}

上面只触发完成块一次,它不会重复。

如何让块重复以便计数递增?

如果我让它工作并将我的动画放入重复块中, stopLoading:如何再次停止动画?

感谢您提供的任何帮助:)

I wanted to do a small loader animation to put in my app. I have done repeating animations before with CGAnimations without problems, this time I was going for the block approach.

I am doing a small test but can make the following code repeat:

- (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 {

}

The above only fires the completion block once, it does not repeat.

How do I get the block to repeat so that count increments?

If I get this working and put my animation into the repeating block,
What goes into stopLoading: to stop the animation again?

Thanks for any help given:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风疏影 2024-11-26 00:10:38

这是一个有限的重复动画:

- (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),我们应该没问题。

Here's a finite repeating animation:

- (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];
}

There's a recursion here, so we don't want to go overboard or we'll run out of memory, but if we limit our count (in your example it was 5) we should be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文