首页 > 代码库 > timer 在滚动的时候停止了的解决办法
timer 在滚动的时候停止了的解决办法
方法一:
-(void) viewDidLoad{
[self performSelectorInBackground:@selector(call1) withObject:nil];
}
-(void) call1{
timer1 = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(doSomething) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:timer1 forMode:NSRunLoopCommonModes];
}
-(void) call2{
// do something
timer1 invalidate];
timer1 = nil;
}
方法二:
-(void)viewDidAppear:(BOOL)animated {
NSThread* timerThread =[[NSThread alloc] initWithTarget:self selector:@selector(timerStart)object:nil];
[timerThread start];}-(void)timerStart
{
NSAutoreleasePool*pool =[[NSAutoreleasePool alloc] init];
NSRunLoop* runLoop =[NSRunLoop currentRunLoop];
timer =[[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(method) userInfo:nil repeats:YES] retain];//一定要retain,不然timerStart执行完后,NSTimer就被释放了。
[runLoop run];
[pool release];}-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[timer invalidate];}
方法三:
timer = [NSTimer timerWithTimeInterval:5.0 target:self selector:@selector(SendHeartBeat) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:heartTimer forMode:NSDefaultRunLoopMode];
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。