繁体   English   中英

ios 中是否有计时器

[英]is there timer in ios

我想创建一个计时器,例如计数 2 秒,然后每秒传递一个 nslog 类型,例如 1 秒

任何建议这样做

是的,有 NSTimer,将其用作 -

[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector() userInfo:nil repeats:NO];

你所追求的是NSTimer

其中,我不能不指出,即使是对框架文档的粗略搜索也会出现。 懒惰是程序员的三大美德之一,但来吧。

你可以像这样调用你的 NSTimer

[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeValue) userInfo:nil repeats:YES];

changeValue function 可以像

-(void)changeValue{
    NSLog("calling function after every two seconds");
}

您可能想要使用NSTimertimerWithTimeInterval:target:selector:userInfo:repeats:方法。 指向一些 object ,它实现了一个打印日志条目的选择器。

是的,ios 具有定时器,用于定期调用方法或按钮触摸事件。 我们可以像下面这样使用它:

[NSTimer scheduledTimerWithTimeInterval:1.0
    target:self
    selector:@selector(Your_target_Method)
    userInfo:nil
    repeats:NO];
NSInteger timer=0;

现在每 1 秒调用一次方法

-(void)Your_target_Method{

timer=timer+1;

NSLog(@"Timer:%ld",timer);

}

此外,请访问https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Timers/Timers.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM