ChenYilong / ChenYilong/iOSInterviewQuestions

30. 以+ scheduledTimerWithTimeInterval...的方式触发的timer,在滑动页面上的列表时,timer会暂定回调,为什么?如何解决?

Open
#107 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
9.5k
Forks
2.8k
PR merge metrics
No merged PRs in 30d

Description

最终答案中只需要
```
//创建 Timer,然后添加到 NSRunLoopCommonModes
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0
target:self
selector:@selector(timerTick:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
```
就能解决 Timer 计时会被 scrollView 的滑动影响。

增加下面的代码会导致无滑动事件时,计时器回调两次,因为 NSRunLoopCommonModes 包括了 UITrackingRunLoopMode 和 NSDefaultRunLoopMode

```
//将timer添加到NSDefaultRunLoopMode中
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timerTick:)
userInfo:nil
repeats:YES];
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.