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

Why timer stops when scrolling in UIWebView iPhone?(为什么在 UIWebView iPhone 中滚动时计时器停止?)

本文介绍了为什么在 UIWebView iPhone 中滚动时计时器停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

I am having a bit of trouble figuring out why the NSTimer stops as soon as I scroll in the UIWebView. Please take a look at the following code (I know this isn't a homework site, but I honestly am stuck):

- (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];
}

推荐答案

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

You have to add the timer to another RunLoopMode. Per default a timer is added to the NSDefaultRunLoopMode. That means that the timer is only executing when the app’s run loop is in NSDefaultRunLoopMode.

现在,当用户触摸屏幕(例如滚动 UIScrollView)时,运行循环的模式将切换到 NSEventTrackingRunLoopMode.现在,运行循环不再处于 NSDefaultRunMode 中,计时器将不会执行.这样做的丑陋效果是,每当用户触摸屏幕时,计时器就会被阻止.当用户滚动时,这可能是一个很长的时间,因为计时器被阻塞,直到滚动完全停止.而当用户继续滚动时,定时器又被阻塞了.

Now, when a user touches the screen (e.g. to scroll a UIScrollView) the run loop’s mode will be switched to NSEventTrackingRunLoopMode. And now, that the run loop is not in NSDefaultRunMode anymore, the timer will not execute. The ugly effect of that is, that timer is blocked whenever the user touches the screen. And that can be a looong time when the user is scrolling, because the timer is blocked until the scrolling completely stops. And when the user continues to scroll, the timer is blocked again.

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

Fortunately the solution to this problem is quite simple: You can add your timer to another NSRunLoopMode. When add the timer to NSRunLoopCommonModes it will execute in all run loop modes (that have been declared as a member of the set of "common" modes, to be precise). That means that the timer is not only working in NSDefaultRunLoopMode but also in NSEventTrackingRunLoopMode (when the user touches the screen).

所以在你初始化你的定时器之后,将它添加到 NSRunLoopCommonModes:

So after you initialize your timer, add it to NSRunLoopCommonModes:

[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

这篇关于为什么在 UIWebView iPhone 中滚动时计时器停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么在 UIWebView iPhone 中滚动时计时器停止?

基础教程推荐