Pausing an NSTimer

后端 未结 4 1450
孤城傲影
孤城傲影 2021-01-15 19:14

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

4条回答
  •  无人及你
    2021-01-15 19:44

    Create a flag (BOOL) to control the updating of the image.

    Clear it when you want the routine that updates the image to ignore the fact that it was called by the timer event firing. When you want the image updates to resume, just set the flag. Maybe something like this:

    -(void)ImageUpdater:(NSTimer *)theTimer
    {
      if(image_updates_enabled)
      {
         // Code to update image
      }
    }
    

    Since you are firing a method only every three seconds it isn't a big deal to just let the timer run.

    The other thing you can to is to setup the timer so that it does not repeat. With that in place the decision to re-enable the timer could be made somewhere else in your code rather than automatically. You could even do it within the callback function and still use the "image_updates_enabled" flag if you wish.

提交回复
热议问题