Tricky
Tricky

Reputation: 7155

CADisplayLink stops updating when UIScrollView scrolled

Title is quite self explanatory, but I have some animation being done in a loop triggered by CADisplayLink. However, as soon as I scroll a UIScrollView I have added to my view hierarchy, the animation stops immediately, only to return again when scrolling has completely stopped and come to a standstill....

Anyway to cancel this behaviour?

Upvotes: 19

Views: 9411

Answers (8)

ooOlly
ooOlly

Reputation: 2127

Actually CADisplayLink support multiple RunloopMode. try this:

    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:UITrackingRunLoopMode];
    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

Upvotes: 4

eonil
eonil

Reputation: 86015

Use UITrackingRunLoopMode. It's specifically designed for scrolling stuffs.

Otherwise, just call render & present routine at -scrollViewDidScroll.

UIScrollView broken and halts scrolling with OpenGL rendering (related CADisplayLink, NSRunLoop)

Upvotes: 7

Maurizio
Maurizio

Reputation: 4169

We fixed this same issue by changing the frameInterval from 1, to 2. This essentially halves the rendering rate of your OpenGL scene, but it still may render sufficiently for your needs.

frameInterval The number of frames that must pass before the display link notifies the target again.

@property(nonatomic) NSInteger frameInterval Discussion The default value is 1, which results in your application being notified at the refresh rate of the display. If the value is set to a value larger than 1, the display link notifies your application at a fraction of the native refresh rate. For example, setting the interval to 2 causes the display link to fire every other frame, providing half the frame rate.

Setting this value to less than 1 results in undefined behavior and is a programmer error.

Upvotes: 1

Ricardo Sanchez-Saez
Ricardo Sanchez-Saez

Reputation: 9566

Here you can find a better (and more complex) solution:

Animation in OpenGL ES view freezes when UIScrollView is dragged on iPhone

that allows you to use 'NSRunLoopCommonModes' and avoids OpenGL freezing when holding a finger without scrolling.

This is related to what Doug found (setting the frame interval of CADisplayLink to 2 instead of 1 fixing UIScrollView).

Upvotes: 4

Doug
Doug

Reputation: 11

I found that if I set the frame interval to 2 instead of 1 (so 30 frames a second) everything works fine. So what I'm doing is setting it to 2 when my popover comes up and resetting it to 1 when it dismisses.

Upvotes: 1

Doug
Doug

Reputation: 21

NSRunLoopCommonModes seems to mess with the bounciness and continuous nature of the uiscrollview.

Upvotes: 2

nornagon
nornagon

Reputation: 15821

You can also mitigate the effects of this issue by using NSRunLoopCommonModes instead of NSDefaultRunLoopModes:

[displayLink addToRunLoop:[NSRunLoop currentRunLoop]
                  forMode:NSRunLoopCommonModes];

Upvotes: 34

Steven Canfield
Steven Canfield

Reputation: 7332

Run the display link (using -addToRunLoop:forMode:) on another thread with another run loop. So create a new thread, create a run loop on that thread, and run the CADisplayLink on that thread/run loop.

Upvotes: 7

Related Questions