取消 UIView animateWithDuration 中的块

2024-01-10

- (void) startLoading {

    [self blink];
} 

 - (void) blink {  

        [UIView animateWithDuration:0.5
                              delay: 0.0
                            options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseOut
                         animations:^{
                            //animate stuff
                         }
                         completion:^(BOOL finished){
                                 [self blink];    
                         }];

}

- (void) stopLoading {
    //What goes here?
}

在我的 UIView 中initWithFrame,我构建了一些加载程序图形,然后从以下位置启动加载程序动画[self startLoading].

现在的问题是,如何停止这个“无限循环”? or stopLoading 或 dealloc 方法中使用什么来很好地拆除所有内容?

当我忽略完成块存在的事实并只是从超级视图中释放我的 UIView 时,一切都会正常运行几秒钟(超过指定的 0.5 秒),然后应用程序崩溃并显示一条消息:

malloc: * mmap(size=2097152)失败(错误代码=12)错误:无法分配区域 *


要取消,请执行您要执行的操作any您希望能够取消的循环操作:设置一个标记,每次在循环之前检查该标记。所以:

- (void) stopLoading {
    kCancel = YES;
}

现在你的完成块看起来像这样:

completion:^(BOOL finished){
    if (!kCancel)
        [self blink];    
}];

kCancel可以是 ivar、静态全局变量等等。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

取消 UIView animateWithDuration 中的块 的相关文章

随机推荐