10

I want to mimic the UIScrollView behavior where the current thread RunLoop mode changes to UITrackingRunLoopMode when you drag the scrollView, and back to NSDefaultRunLoopMode when you stop dragging.

I want to do it with my own class, though, dragging around views... Right now I'm using this code

while (_draggingView && [[NSRunLoop currentRunLoop] runMode:UITrackingRunLoopMode beforeDate:[NSDate distantFuture]]);

When I stop dragging, _draggingView changes to nil so I stop looping in that mode. Is there a more elegant/better way of doing this?

How is UIScrollView doing it? Input sources?

Thank you

LocoMike
  • 5,626
  • 5
  • 30
  • 43
  • I fear that it is not possible to change the run loop mode without using some private API. Could you elaborate more what are you trying to do? – sergio May 31 '12 at 14:09
  • why do you want to change the mode of the runloop? is there any real problem or you want to learn more about the runloop? – Tushar Nov 08 '16 at 06:43

2 Answers2

4

You can use CFRunLoopStop to force a run loop to return.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    CFRunLoopStop(CFRunLoopGetMain());
}
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 1
    I don't want to stop a runLoop. I just want to change the mode. – LocoMike Jun 01 '12 at 17:55
  • You can't change the mode. You run it in a mode. When you want to run in a different mode, you stop the current run and start again in the new mode (or just return and let the outer invocation of `CFRunLoopRun` resume). – rob mayoff Jun 01 '12 at 18:44
-1

You can use Decelerated methods.

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

After we drag the scroll view, it starts decelerating, so, these methods might be of use to you.

Paresh Thakor
  • 1,795
  • 2
  • 27
  • 47
  • No, that has no use. I have no UISCrollView, I just want to change the NSRunLoop of the main thread to work in UITrackingRunLoopMode whenever I want (Just like UIScrollView does when you drag). But thanks anyway. – LocoMike May 29 '12 at 15:34