暂停NSTimer [英] Pausing an NSTimer

查看:78
本文介绍了暂停NSTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 UIView ,带有 UITableView UIImageView 在里面。 UITableView 占据 UIView 的上半部分。 UIImageView 占据 UIView 的下半部分。

I have a UIView with a UITableView and a UIImageView in it. The UITableView takes up the top half of the UIView. The UIImageView takes the bottom half of the UIView.

我使用 NSTimer 来更新 UIImageView 中显示的图像。基本上,每3秒, UIImageView 加载一个新的 UIImage

I am using an NSTimer to update the image that is being displayed in the UIImageView. Basically, every 3 seconds, the UIImageView loads in a new UIImage.

问题是,如果我从 UITableView 中选择一行, NSTimer 继续运行。如何在 didSelectRowFromIndexPath 上停止 NSTimer ,然后在视图返回到屏幕时再次启动它?

Problem is, if I select a row from the UITableView, the NSTimer keeps running. How can I stop the NSTimer on didSelectRowFromIndexPath, and then start it again when the view returns to the screen?

-(void)viewDidLoad {
NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread) object:nil]; //Create a new thread
    [timerThread start]; //start the thread

} // end viewDidLoad

//
-(void) startTimerThread {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
    [[NSTimer scheduledTimerWithTimeInterval: 3.0
                                      target: self
                                    selector: @selector(loadNextPhoto)
                                    userInfo: nil
                                     repeats: YES] retain];

    [runLoop run];
    [pool release];
}


推荐答案

保留对计时器的引用你创建,所以你可以在需要时停止它。下次需要时,您可以创建一个新的计时器。这听起来像你最好的地方是在viewDidAppear:和viewWillDisappear:。我不确定你为什么要在新线程上启动计时器。你可能有充分的理由这样做但我从来不需要这样做。

Keep a reference to the timer you create so you can stop it when needed. The next time you need it you can just create a new timer. It sounds like the best places for you to do this are in viewDidAppear: and viewWillDisappear:. I'm not sure why you are starting the timer on a new thread. You may have a good reasons for doing so but I have never needed to do this.

//This code assumes you have created a retained property for loadNextPhotoTimer

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        [self startTimer];
    }

    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [self stopTimer];
    }

    - (void)startTimer {
        NSTimer *timer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:3.0] interval:3.0 target:self selector:@selector(loadNextPhoto) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        self.loadNextPhotoTimer = timer;
        [timer release];
    }

    - (void)stopTimer {
        [self.loadNextPhotoTimer invalidate];
        self.loadNextPhotoTimer = nil;
    }

这篇关于暂停NSTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆