UIScrollview中的事件跟踪会阻止主线程.我正在使用主线程运行一个驱动某些动画的计时器 - 结果是任何用户与可滚动视图的交互(向上或向下拖动等)都会导致动画(在主runloop上运行)冻结.有没有解决的办法?
我已经尝试过关于NSRunloop的RTFM(CFRunLoopAddCommonMode等),但它非常简洁,让我相信可以更好地避免修改事件优先级/线程优先级.有人有任何见解吗?
Joh*_*hen 19
而不是使用NSTimer的静态构造函数之一,创建计时器对象并手动为"常见"运行循环模式安排它:
迅速
let timer = Timer.init(timeInterval: 10, repeats: false) { (_) in
// ... do something useful after 10 seconds ...
}
RunLoop.main.add(timer, forMode: .commonModes)
Run Code Online (Sandbox Code Playgroud)
目标C.
NSTimer *timer = [[NSTimer alloc] initWithFireDate: [NSDate dateWithTimeIntervalSinceNow: delayInSeconds] interval: 0 target:yourObject selector:yourSelector userInfo:nil repeats:NO];
[NSRunLoop.mainRunLoop addTimer: timer forMode: NSRunLoopCommonModes];
Run Code Online (Sandbox Code Playgroud)
构建你的paramters时,由于您使用的是重复定时器,定时器会有所不同,但其基本思想是手动调度NSRunLoopCommonModes计时器.
发生的事情是当你滚动时,运行循环进入一种模式,阻止"默认模式"任务执行.静态NSTimer函数全部调度到默认模式,这就是为什么它们在您滚动时不会触发的原因.手动调度计时器允许您指定模式,NSRunLoopCommonModes包括滚动使用的运行模式.