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

查看:264
本文介绍了为什么在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中滚动时计时器停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆