为什么在UIWebView iPhone中滚动时计时器停止?

dya*_*ang 6 iphone cocoa-touch uiwebview nstimer

可能重复:
UIScrollView暂停NSTimer直到滚动完成

我在UIWebView中滚动时,弄清楚为什么NSTimer会停止,我有点麻烦.请看下面的代码(我知道这不是一个家庭作业网站,但老实说我卡住了):

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *address = @"http://en.m.wikipedia.org/wiki/";
    NSURL *url = [NSURL URLWithString:address];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [wiki loadRequest:request];
    time = 0;
    countValue = 0;
    timer = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(updateTimer) userInfo: nil repeats: YES];
    timeRand = 0;
    countValueRand = 1;
    randomTimer = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(updateRandomTimer) userInfo: nil repeats: YES];
    [self generateWordsArray];
}
Run Code Online (Sandbox Code Playgroud)

joe*_*ern 30

您必须将计时器添加到另一个RunLoopMode.默认情况下,计时器将添加到NSDefaultRunLoopMode.这意味着定时器仅在应用程序的运行循环在NSDefaultRunLoopMode中时执行.

现在,当用户触摸屏幕时(例如,滚动UIScrollView),运行循环的模式将切换到NSEventTrackingRunLoopMode.现在,运行循环不再在NSDefaultRunMode中,计时器将不会执行.其丑陋的效果是,只要用户触摸屏幕,该计时器就会被阻止.当用户滚动时,这可能是一个漫长的时间,因为计时器被阻止,直到滚动完全停止.当用户继续滚动时,计时器再次被阻止.

幸运的是,这个问题的解决方案非常简单:您可以将计时器添加到另一个NSRunLoopMode.将计时器添加到NSRunLoopCommonModes时,它将在所有运行循环模式中执行(确切地说,已声明为"常用"模式集的成员).这意味着计时器不仅在NSDefaultRunLoopMode中工作,而且在NSEventTrackingRunLoopMode中工作(当用户触摸屏幕时).

因此,在初始化计时器后,将其添加到NSRunLoopCommonModes:

[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
Run Code Online (Sandbox Code Playgroud)

  • 你,我的朋友,真棒! (5认同)