首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

#nstimer

如何使用NSTimer?

幽幽77IT从业者
有一些使用计时器的方法: 1)预定定时器和使用选择器 NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector:@selector(onTick:) userInfo: nil repeats:NO]; 如果将重复设置为NO,则定时器将在运行选择器之前等待2秒钟,之后将停止; 如果重复:YES,定时器将立即启动,并将每2秒重复一次调用选择器; 停止定时器,你调用定时器的无效方法:[t invalidate]; 作为一个方面的说明,而不是使用一个不重复,并在指定的时间间隔后调用选择器的计时器,你可以使用这样一个简单的语句: [self performSelector:@selector(onTick:) withObject:nil afterDelay:2.0]; 这将与上面的示例代码具有相同的效果; 但是如果要每隔第n次调用一次选择器,则使用带有重复的定时器:YES; 2)自我计划的计时器 NSDate *d = [NSDate dateWithTimeIntervalSinceNow: 60.0]; NSTimer *t = [[NSTimer alloc] initWithFireDate: d interval: 1 target: self selector:@selector(onTick:) userInfo:nil repeats:YES]; NSRunLoop *runner = [NSRunLoop currentRunLoop]; [runner addTimer:t forMode: NSDefaultRunLoopMode]; [t release]; 这将创建一个计时器,它将在您指定的自定义日期(在这种情况下,一分钟后)自行启动,并且每隔一秒重复一次 3)计划外的计时器和使用调用 NSMethodSignature *sgn = [self methodSignatureForSelector:@selector(onTick:)]; NSInvocation *inv = [NSInvocation invocationWithMethodSignature: sgn]; [inv setTarget: self]; [inv setSelector:@selector(onTick:)]; NSTimer *t = [NSTimer timerWithTimeInterval: 1.0 invocation:inv repeats:YES]; 在此之后,您需要像这样手动启动计时器: NSRunLoop *runner = [NSRunLoop currentRunLoop]; [runner addTimer: t forMode: NSDefaultRunLoopMode]; 注意,onTick:方法如下所示: -(void)onTick:(NSTimer *)timer { //do smth }... 展开详请
有一些使用计时器的方法: 1)预定定时器和使用选择器 NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector:@selector(onTick:) userInfo: nil repeats:NO]; 如果将重复设置为NO,则定时器将在运行选择器之前等待2秒钟,之后将停止; 如果重复:YES,定时器将立即启动,并将每2秒重复一次调用选择器; 停止定时器,你调用定时器的无效方法:[t invalidate]; 作为一个方面的说明,而不是使用一个不重复,并在指定的时间间隔后调用选择器的计时器,你可以使用这样一个简单的语句: [self performSelector:@selector(onTick:) withObject:nil afterDelay:2.0]; 这将与上面的示例代码具有相同的效果; 但是如果要每隔第n次调用一次选择器,则使用带有重复的定时器:YES; 2)自我计划的计时器 NSDate *d = [NSDate dateWithTimeIntervalSinceNow: 60.0]; NSTimer *t = [[NSTimer alloc] initWithFireDate: d interval: 1 target: self selector:@selector(onTick:) userInfo:nil repeats:YES]; NSRunLoop *runner = [NSRunLoop currentRunLoop]; [runner addTimer:t forMode: NSDefaultRunLoopMode]; [t release]; 这将创建一个计时器,它将在您指定的自定义日期(在这种情况下,一分钟后)自行启动,并且每隔一秒重复一次 3)计划外的计时器和使用调用 NSMethodSignature *sgn = [self methodSignatureForSelector:@selector(onTick:)]; NSInvocation *inv = [NSInvocation invocationWithMethodSignature: sgn]; [inv setTarget: self]; [inv setSelector:@selector(onTick:)]; NSTimer *t = [NSTimer timerWithTimeInterval: 1.0 invocation:inv repeats:YES]; 在此之后,您需要像这样手动启动计时器: NSRunLoop *runner = [NSRunLoop currentRunLoop]; [runner addTimer: t forMode: NSDefaultRunLoopMode]; 注意,onTick:方法如下所示: -(void)onTick:(NSTimer *)timer { //do smth }

游戏语音GVoice的ios没有回调是什么原因?

领券