簡體   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