29,028
社区成员
发帖
与我相关
我的任务
分享
#import "ShowViewController.h"
@interface ShowViewController ()
//
@property (nonatomic,strong) NSTimer *timer;
//
@property (nonatomic,strong) NSThread *thread;
@end
@implementation ShowViewController
- (void)dealloc{
NSLog(@"销毁");
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
self.navigationItem.leftBarButtonItem = leftBtn;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
__weak typeof(self) weakSelf = self;
self.thread = [[NSThread alloc] initWithTarget:weakSelf selector:@selector(openTimer) object:nil];
[_thread start];
}
// 返回
- (void)back{
if (_timer && _thread) {
[self performSelector:@selector(deleteTimer) onThread:_thread withObject:nil waitUntilDone:NO];
[self performSelector:@selector(stopThread) onThread:_thread withObject:nil waitUntilDone:NO];
_thread = nil;
}
NSLog(@"%@",self);
[self.navigationController popViewControllerAnimated:YES];
NSLog(@"%@",self);
}
// 停止线程
- (void)stopThread{
CFRunLoopStop(CFRunLoopGetCurrent());
}
//
- (void)openTimer{
@autoreleasepool {
__weak typeof(self) sSelf = self;
if (!_timer) {
self.timer = [NSTimer timerWithTimeInterval:1 target:sSelf selector:@selector(click) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] run];
}
}
}
- (void)click{
NSLog(@"1");
}
- (void)deleteTimer{
[self.timer invalidate];
self.timer = nil;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@",[NSThread currentThread]);
__weak __typeof(self) weakSelf = self;
//创建子线程
dispatch_async(dispatch_get_global_queue(0, 0), ^{
__strong __typeof(weakSelf) strongSelf = weakSelf;
if (strongSelf) {
strongSelf.thread1 = [NSThread currentThread];
[strongSelf.thread1 setName:@"线程A"];
//在子线程中创建NSTimer
strongSelf.threadTimer = [NSTimer timerWithTimeInterval:1 target:strongSelf selector:@selector(timerAction) userInfo:nil repeats:YES];
//将NSTimer加入到子线程的RunLoop中
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
[runloop addTimer:strongSelf.threadTimer forMode:NSDefaultRunLoopMode];
//手动启动RunLoop
[runloop run];
NSLog(@"###########");
[NSThread sleepForTimeInterval:2];
[strongSelf.threadTimer invalidate];
}
});
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (self.threadTimer) {
[self.threadTimer invalidate];
self.threadTimer = nil;
}
}
}
[runloop run]; 后面的代码 [strongSelf.threadTimer invalidate];根本就没有执行,所以就没法停止timer,而调用touchesBegan:withEvent:方法里的[self.threadTimer invalidate];才可以停止timer。请问怎么解决呢?
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self deleteTimer];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
__weak typeof(self) weakSelf = self;
// self.thread = [[NSThread alloc] initWithTarget:weakSelf selector:@selector(openTimer) object:nil];
// [_thread start];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(click) userInfo:nil repeats:YES];
}