nstimer 如何停止

languang56 2016-11-27 06:32:58
我写了一个定时器
-(void)stopTimer
{
//开始拖动scrollview的时候 停止计时器控制的跳转
[timer invalidate];
timer = nil;
}

-(void)startTimer
{
if( [timer isValid] )
{
[timer invalidate];
timer = nil;
}
//拖动完毕的时候 重新开始计时器控制跳转
timer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(nextPage:) userInfo:nil repeats:YES];
}

写了一个开始与停止方法
但不是很管用 正常使用的时候好用 但有时会 一直创建新的无法停止 有解决办法吗?
...全文
411 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
看_灰机 2016-11-29
  • 打赏
  • 举报
回复
http://blog.csdn.net/w582324909/article/details/52582522
yangjian1821 2016-11-29
  • 打赏
  • 举报
回复
//拖动完毕的时候 重新开始计时器控制跳转 timer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(nextPage:) userInfo:nil repeats:YES]; 个人感觉问题出在这了,,你每次虫死startTimer 的时候 timer 都重新初始化一遍 所以即使上次的timer 停止了 你还有继续运行的timer。 这样写试试 if(timer == nil) { timer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(nextPage:) userInfo:nil repeats:YES]; }
lion_witcher 2016-11-29
  • 打赏
  • 举报
回复
应该跟你调用的方法有关, 这样写的开和关, 没什么问题

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧