NSTimer not fired when uiscrollview event occurs

前端 未结 2 440
野趣味
野趣味 2020-12-01 08:35

I have a UIImageView placed in UIScrollView, Basicly this UIImageView holds very big map, and created animation on a predefined path with \"arrows\" pointed navigation direc

相关标签:
2条回答
  • 2020-12-01 08:59

    One more thing (c)

    1. use timerWithTimeInterval method in order to avoid adding timer to runloop in DefaultMode
    2. use mainRunLoop

    So:

    self.myTimer = [NSTimer timerWithTimeInterval:280 target:self selector:@selector(doStuff) userInfo:nil repeats:NO]; [[NSRunLoop mainRunLoop] addTimer:self.myTimer forMode:NSRunLoopCommonModes];

    0 讨论(0)
  • 2020-12-01 09:03

    iOS Applications run on an NSRunLoop. Each NSRunLoop has different modes of execution for different tasks. For example, the default nstimer is scheduled to run under the NSDefaultRunMode on the NSRunLoop. What this means however is that certain UIEvents, scrollviewing being one, will interrupt the timer, and place it on a queue to be run as soon as the event stops updating. In your case, in order to get the timer to not be interrupted, you need to schedule it for a different mode, namely NSRunLoopCommonModes, like so:

      self.myTimer =  [NSTimer scheduledTimerWithTimeInterval:280
                                                                     target:self
                                                                   selector:@selector(doStuff)
                                                                   userInfo:nil
                                                                    repeats:NO];
      [[NSRunLoop currentRunLoop] addTimer:self.myTimer forMode:NSRunLoopCommonModes]; 
    

    This mode will allow your timer to not be interrupted by scrolling. You can find more about this info here: https://developer.apple.com/documentation/foundation/nsrunloop At the bottom you will see the definitions of the modes you can choose from. Also, legend has it, you can write your own custom modes, but few have ever lived to tell the tale im afraid.

    0 讨论(0)
提交回复
热议问题