我在 UITTableViewCell 中运行我的动画。每个单元格都有自己的动画,并且单元格是可重复使用的。我用[mView performSelectorInBackground:@selector(layoutSubview) withObject:nil];
在后台线程中,我启动 runLoop 以执行如下任务:
- (void)startAnimation
{
NSRunLoop *mLoop = [NSRunLoop currentRunLoop];
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];
mRunLoop = YES;
while (mRunLoop == YES && [mLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.01]]);
}
并停止它:
- (void)stopAnimation
{
if (![NSThread isMainThread]) {
[[NSThread currentThread] cancel];
}
mRunLoop = NO;
self.animationTimer = nil;
CFRunLoopStop(CFRunLoopGetCurrent());
}
当我快速滚动表格时遇到问题,因为在第一个单元格启动时我开始动画,所以第一个 runLoop 调用发生,它执行 setNeedDisplay 和它的所有方法。但是在完成第一个 runLoop 循环之前,单元格从视图中消失并且已经可以重用了。所以我开始清除它,而循环仍在执行操作,在这里我遇到了这样的情况
发送到已释放实例的消息
那么您能否给我一些提示,说明我应该如何正确停止在该线程中执行操作?我的意思是,如果我想了解例如一个正在执行某些操作的对象,如何立即停止它们?
希望我提供了足够的信息。谢谢
更新:完全没有想法?