将当前线程NSRunLoop设置为以某种模式运行的正确方法是什么,直到我决定停止它为止?

Loc*_*ike 10 uiscrollview nsrunloop ios

我想模仿UIScrollView行为,当你拖动scrollView时当前线程RunLoop模式更改为UITrackingRunLoopMode,并在停止拖动时返回NSDefaultRunLoopMode.

我想用我自己的类来做,但是拖着视图......现在我正在使用这段代码

while (_draggingView && [[NSRunLoop currentRunLoop] runMode:UITrackingRunLoopMode beforeDate:[NSDate distantFuture]]);
Run Code Online (Sandbox Code Playgroud)

当我停止拖动时,_draggingView更改为nil,因此我停止在该模式下循环.这样做有更优雅/更好的方式吗?

UIScrollView是如何做到的?输入源?

谢谢

rob*_*off 4

您可以使用CFRunLoopStop强制运行循环返回。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    CFRunLoopStop(CFRunLoopGetMain());
}
Run Code Online (Sandbox Code Playgroud)