以+scheduledTimerWithTimeInterval...的方式触发的timer,在滑动页面上的列表时,ti

以+scheduledTimerWithTimeInterval...的方式触发的timer,在滑动页面上的列表时,timer会暂定回调,为什么?如何解决?
这里强调一点:在主线程中以+scheduledTimerWithTimeInterval...的方式触发的timer默认是运行在NSDefaultRunLoopMode模式下的,当滑动页面上的列表时,进入了UITrackingRunLoopMode模式,这时候timer就会停止

可以修改timer的运行模式为NSRunLoopCommonModes,这样定时器就可以一直运行了


方法内部已经创建NSTimer对象,并加入到RunLoop中,运行模式为NSDefaultRunLoopMode由于Mode有timer对象,所以RunLoop就开始监听定时器事件了,从而开始进入运行循环这个方法仅仅是创建RunLoop对象,并不会主动启动RunLoop,需要再调用run方法来启动

如果在主线程中通过scheduledTimerWithTimeInterval:...方法来构建NSTimer,就不需要主动启动RunLoop对象,因为主线程的RunLoop对象在程序运行起来就已经被启动了

// userInfo参数:用来给NSTimer的userInfo属性赋值,userInfo是只读的,只能在构建NSTimer对象时赋值[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(run:) userInfo:@"ya了个hoo" repeats:YES];// scheduledTimer...方法创建出来NSTimer虽然已经指定了默认模式,但是【允许你修改模式】[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];// 【仅在子线程】需要手动启动RunLoop对象,进入运行循环[[NSRunLoop currentRunLoop] run];