cocoa-touch - 重复 UIAnimation block ,以及再次停止它的方法

标签 cocoa-touch ios4 core-animation objective-c-blocks uiviewanimation

我想做一个小的加载器动画来放入我的应用程序。我以前用 CGAnimations 做过重复动画没有问题,这次我要用 block 方法。

我正在做一个小测试,但可以重复以下代码:

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

}

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

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

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

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

最佳答案

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

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

关于cocoa-touch - 重复 UIAnimation block ,以及再次停止它的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6616213/

相关文章:

iphone - 如何防止UIView的drawRect清除之前drawRect调用绘制的路径?

objective-c - 可以在 UIPopoverController 中呈现没有 UIViewController 的 UIView 吗?

iOS - 无法在具有比例变换的 UIViewAnimation 期间点击 UIView

ios - 带有来自 patternImage 的颜色的 CAKeyFrameAnimation

swift - 仅适用于 super View 的动画变换

ios - 我想为按钮和下面的方法编写一个OCMock单元测试。我该怎么写?

ios - 直接在代码中获取/设置属性时,Core Data 不会自动调用值转换器

objective-c - 如何生成在 UIPickerView 中使用的整数数组?

ios - UITableViewCell 高度在每次旋转变化时增加 2 个像素

iphone - 为什么我的应用从 App Store 下载时不要求使用用户的当前位置?