在UIView animateWithDuration中取消块 [英] Cancel block in UIView animateWithDuration

查看:744
本文介绍了在UIView animateWithDuration中取消块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- (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] 开始加载动画。

In my UIView's initWithFrame, I build some loader graphics then start the loader animation from [self startLoading].

问题是,如何阻止这个无限循环? stopLoading或dealloc方法中的内容能很好地删除所有内容?

当我忽略一个完成块,并且只是从超级视图释放我的UIView的事实,一切都进行好几秒钟(超过指定的0.5秒),然后应用程序崩溃与消息: p>

When I ignore the fact that a completion block is there and just release my UIView from the super view, everything goes fine for some seconds (more than the 0.5 sec specified) then the app crashes with a message:


malloc: * mmap(size = 2097152)失败(错误代码= 12)
错误:无法分配区域
*
*在malloc_error_break中设置断点以调试

malloc: * mmap(size=2097152) failed (error code=12) error: can't allocate region ** set a breakpoint in malloc_error_break to debug

malloc_error_break中的断点和动画块。

I have a breakpoint in malloc_error_break and the culprit is the animation block.

我假设UIView是通过从超级视图中删除而释放的,稍后在完成块被执行时, self 这是一个发布对象的消息。

I assume that the UIView was released by being removed from the super view and later on the completion block is executed, having a reference to self this is messaging a released object.

我在文档中找不到任何关于取消

I can't find anything in the docs about canceling a 'queued' block.

推荐答案

要取消,请使用任何循环操作你想要能够取消:设置一个您在循环之前每次检查的标志。所以:

To cancel, do what you would do with any looping operation you want to be able to cancel: set a flag which you check each time before looping. So:

- (void) stopLoading {
    kCancel = YES;
}

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

And now your completion block looks like this:

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

kCancel 可以是ivar,静态全局变量,无论如何。

kCancel could be an ivar, a static global variable, whatever.

这篇关于在UIView animateWithDuration中取消块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆