iOS 基础篇12- 定时器的使用

来源:互联网 发布:虚拟充值软件 编辑:程序博客网 时间:2024/05/01 20:23

1.方法一

@interface ViewController ()@property (nonatomic, weak) NSTimer *timer;@end@implementateion ViewController- (void)startTimer() {    //第一种用法,要手动加入到NSRunLoop中     NSTimer *time= [NSTimer timerWithTimeInterval:3 target:self selector:@selector(timerFire) userInfo:nil repeats:YES];    [[NSRunLoop currentRunLoop] addTimer:time forMode:NSDefaultRunLoopMode];//手动加入到NSRunLoop中    [time fire];        //开始定时器}- (void) printFire {    NSLog(@"Fire");} - (void)stopTimer(){    [timer invalidate]; //停止定时器}

2.方法二

@interface ViewController ()@property (nonatomic, weak) NSTimer *timer;@end@implementateion ViewController- (void)startTimer() {        //第二种方法   NSTimer *timer= [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(printFire) userInfo:nil repeats:YES];    [timer fire];       //开始定时器}- (void)printFire {    NSLog(@"Fire");} - (void)stopTimer(){    [timer invalidate];//停止定时器}
0 0
原创粉丝点击