macoscocoadelaynsscrollviewendlessscroll

NSScrollView and manually setting the content offset while scrolling


I am trying to build an endless NSScrollView, i.e. a scroll view that can scroll infinitely in either direction. This is achieved by having a scroll view with fixed dimension which is 'recentered' once it gets too close to either edge, and keeping track of an additional endless offset that is updated when recentering. Apple even demonstrated this approach in a WWDC video for iOS, if I recall correctly.

On iOS everything is working. I perform the recentering logic in -scrollViewDidScroll: and it even works when the scrolling motion is decelerating without breaking the deceleration.

Now for the Mac version. Let me tell you that I'm fairly new to Mac development, so I may simply not have performed these operations in the correct places. I currently have the recentering logic in -reflectScrolledClipView:. When I perform the move operation immediately, however, the scroll view jumps exactly twice as far as I want it to (in this case, to 4000). If I delay the method slightly, it works just as expected.

- (void)reflectScrolledClipView:(NSClipView *)cView
{
    [self recenteringLogic];
    [super reflectScrolledClipView:cView];
}

- (void)recenteringLogic
{
    CGFloat offset = self.documentVisibleRect.origin.y;

    if (offset > 6000) {

        // This makes the scroll view jump to ~4000 instead of 5000.
        [self performSelector:@selector(move) withObject:nil];

        // This works, but seems wrong to me
//        [self performSelector:@selector(move) withObject:nil afterDelay:0.0];
    }
}

- (void)move
{
    [self.documentView scrollPoint:NSMakePoint(0, 4000)];
}

Any ideas on how I could achieve the behavior I want?


Solution

  • I ended up working with [self performSelector:@selector(move) withObject:nil afterDelay:0.0]; and haven't encountered any serious issues with it, despite it seeming a little wrong.