标签:
#import "ViewController.h" @interface ViewController () @property(strong, nonatomic) NSTimer *timer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self addTimer]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self removeTimer]; } /** 自定义添加定时器的方法*/ - (void)addTimer{ NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(mySelector) userInfo:nil repeats:YES ]; //改变(添加)定时器的模式,能够同时处理两件事情 [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes]; self.timer = timer; } /** 自定义的移除定时器的方法*/ - (void)removeTimer{ [self.timer invalidate]; self.timer = nil; } /** 自定义的timer响应方法*/ - (void)mySelector{ NSLog(@"mySelector..."); } @end
标签:
原文地址:http://www.cnblogs.com/iospp/p/5230610.html