风气网

IOS中关于NSTimer使用知多少

2024-09-20来自:本站整理
IOS中关于NSTimer使用知多少

一、初始化方法:有五种初始化方法,分别是
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
- (void)viewDidLoad {
[super viewDidLoad];
//初始化一个Invocation对象
NSInvocation * invo = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(init)]];
[invo setTarget:self];
[invo setSelector:@selector(myLog)];
NSTimer * timer = [NSTimer timerWithTimeInterval:1 invocation:invo repeats:YES];
//加入主循环池中
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
//开始循环
[timer fire];
}

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 invocation:invo repeats:YES];

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
NSTimer * timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(myLog) userInfo:nil repeats:NO]

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myLog:) userInfo:@"123" repeats:YES]

- (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(id)ui repeats:(BOOL)rep

NSTimer * timer = [[NSTimer alloc]initWithFireDate:[NSDate distantPast] interval:1 target:self selector:@selector(myLog:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];

注意:这五种初始化方法的异同:
1、参数repeats是指定是否循环执行,YES将循环,NO将只执行一次。
2、timerWithTimeInterval这两个类方法创建出来的对象如果不用 addTimer: forMode方法手动加入主循环池中,将不会循环执行。并且如果不手动调用fair,则定时器不会启动。
3、scheduledTimerWithTimeInterval这两个方法不需要手动调用fair,会自动执行,并且自动加入主循环池。
4、init方法需要手动加入循环池,它会在设定的启动时间启动。

一、初始化方法:有五种初始化方法,分别是 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo; - (void)viewDidLoad { [super viewDidLoad]; //初始化一个Invocation对象 N...

一、初始化方法:有五种初始化方法,分别是
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
- (void)viewDidLoad {
[super viewDidLoad];
//初始化一个Invocation对象
NSInvocation * invo = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(init)]];
[invo setTarget:self];
[invo setSelector:@selector(myLog)];
NSTimer * timer = [NSTimer timerWithTimeInterval:1 invocation:invo repeats:YES];
//加入主循环池中
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
//开始循环
[timer fire];
}

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 invocation:invo repeats:YES];

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
NSTimer * timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(myLog) userInfo:nil repeats:NO]

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myLog:) userInfo:@"123" repeats:YES]

- (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(id)ui repeats:(BOOL)rep

NSTimer * timer = [[NSTimer alloc]initWithFireDate:[NSDate distantPast] interval:1 target:self selector:@selector(myLog:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];

注意:这五种初始化方法的异同:
1、参数repeats是指定是否循环执行,YES将循环,NO将只执行一次。
2、timerWithTimeInterval这两个类方法创建出来的对象如果不用 addTimer: forMode方法手动加入主循环池中,将不会循环执行。并且如果不手动调用fair,则定时器不会启动。
3、scheduledTimerWithTimeInterval这两个方法不需要手动调用fair,会自动执行,并且自动加入主循环池。
4、init方法需要手动加入循环池,它会在设定的启动时间启动。
http://my.oschina.net/u/2340880/blog/398598

  • [连阮淑19514078115] - 在iOS中NSTimer的优缺点有哪些
    岑相滕::是基于和什么比较呢?NSTimer就是个定时器,不好的地方就是需要强引用当前的使用对象,不过可以通过解耦的方式来解决
  • [连阮淑19514078115] - 在ios中怎么用定时器实现自动循环页面滑动
    岑相滕::+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;使用这个类方法,可以让您在某个规定的时间内进行一个动作(target:(id)和selector:(SEL)),只要在这个动作中设置带有动画切换的uiview页面...
  • [连阮淑19514078115] - iOS后台保活
    岑相滕::iOS后台保活按时间可分为 短时保活 和 长时间保活 如果想申请多一点时间,可以使用NSTimer循环申请保活时间,但是建议不要无限申请保活时间,因为系统如果发现该app一直在后台运行,可能会直接杀掉app。编写音乐播放类:
  • [连阮淑19514078115] - ios开发 拍卖 倒计时怎么做
    岑相滕::使用NSTimer来实现 主要使用的是NSTimer的scheduledTimerWithTimeInterval方法来每1秒执行一次timeFireMethod函数,timeFireMethod进行倒计时的一些操作,完成时把timer给invalidate掉就ok了,代码如下:1 secondsCountDown = 60;\/\/60秒倒计时 2 countDownTimer = [NSTimer scheduledTimerWithTimeInterval...
  • [连阮淑19514078115] - iOS 保证定时器进入后台依然运行
    岑相滕::最近项目中有个 定时器计时 实时更新等车的时长,因为项目里面进入后台是有执行一些任务的操作,因此如果进入后台时间不长,是定时器是不会暂停的,但如果进入后台时间,超过 20s 以上,定时器就暂停,回到前台重新开始倒计时,这时候等车的时长会出现不准的情况。经验证 NSTimer , CADisplayLink , ...
  • [连阮淑19514078115] - iOS interactivePopGestureRecognizer卡住&手势滑动监听
    岑相滕::手势侧滑导航在 iOS 中尤其常见,在项目中,经常和其打交道,但是经常发现,当侧滑到根控制器(第一级界面)时,继续快速使用侧滑手势 popGesture 会导致卡住。有时候,想要监听某个界面的侧滑手势是否结束来做一些其他操作,比如,在一个有 定时器(NSTimer) 的界面,使用侧滑到上一级页面,会导致该...
  • [连阮淑19514078115] - ios如何实现字幕的循环滚动
    岑相滕::您好,具体思路是使用NSTimer的类方法 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; 使用这个类方法,可以让您在某个规定的时间...
  • [连阮淑19514078115] - ios软件开发在关闭了定时器后时间还再ji 是什么意思
    岑相滕::\/\/2.没有在正确的时间停止计时器(两种情况):\/\/-(1)计时器与计时器的执行者耦合时:\/\/-没有在viewWillDisappear情况下停止计时器 \/\/-(2)计时器与计时器的执行者不耦合时:\/\/-没有在dealloc方法停止计时器 \/\/3.如果需要知道timer如何解耦,可以自行百度,或直接问我 \/\/IOS10的SDK当中NSTimer多了...
  • [连阮淑19514078115] - iOS面试题有哪些?
    岑相滕::4、block和代理的通知的区别。block的用法需要注意些什么。5、strong,weak,retain,assign,copy nomatic 等的区别。6、设计模式,mvc,单利,工厂,代理等的应用场景。7、单利的写法。在单利中创建数组应该注意些什么。8、NSString 的时候用copy和strong的区别。9、响应值链。10、NSTimer 在子线程中...
  • [连阮淑19514078115] - iOS开发:请问:下载文件,我知道文件大小以及已经下载的大小,如何求出下...
    岑相滕::然后计算出3秒内的差值,就知道3秒内下载的数据增加了多少,然后就能计算出每秒的增加了多少,这样就知道实时的下载速度了。每3秒读取文件大小计算差值可以用NSTimer实现,虽然NSTimer 因为runloop的关系并不能非常准确的每3秒运行一次,不过误差不会太大,影响不会太大。
  • 首页 热门
    返回顶部
    风记网