我如何使用NSTimer,启动/停止?

Nik*_*how 0 iphone animation timer objective-c nstimer

我如何使用NSTimer每秒进行一次动作?

对尼基塔说.

San*_*eep 5

用这种方式,

-(void)fireTimer{
   [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selected(timeOut:) userInfo:nil repeats:YES];
}
Run Code Online (Sandbox Code Playgroud)

现在,这将每隔1秒调用你的timeOut:选择器,然后在timeOut中调用:

-(void)timeOut:(NSTimer*)timer{
   static int count = 0;
   count++;
   if(count == 60){
     [timer invalidate]; 
     timer = nil;
   }
}
Run Code Online (Sandbox Code Playgroud)

有很多api.我希望您查看https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html.