macos - NSScrollView 并在滚动时手动设置内容偏移量

标签 macos cocoa delay nsscrollview endlessscroll

我正在尝试构建一个无尽的 NSScrollView,即可以在任一方向无限滚动的 ScrollView 。这是通过具有固定尺寸的 ScrollView 来实现的,一旦它太靠近任一边缘,该 ScrollView 就会“重新居中”,并跟踪在重新居中时更新的附加无限偏移量。如果我没记错的话,Apple 甚至在 iOS 的 WWDC 视频中演示了这种方法。

在 iOS 上一切正常。我在 -scrollViewDidScroll: 中执行重新定位逻辑,甚至在滚动运动减速时也能正常工作,而不会破坏减速。

现在推出 Mac 版本。让我告诉你,我对 Mac 开发相当陌生,所以我可能根本没有在正确的位置执行这些操作。我目前在 -reflectScrolledClipView: 中有重新定位逻辑。然而,当我立即执行移动操作时, ScrollView 会准确地跳转到我想要的两倍(在本例中为 4000)。如果我稍微延迟该方法,它就会按预期工作。

- (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)];
}

关于如何实现我想要的行为有什么想法吗?

最佳答案

试试这个:

- (void)scrollWheel:(NSEvent *)event {
    [super scrollWheel:event];
    [self recenteringLogic];
}

- (void)recenteringLogic
{
    NSRect rect = self.documentVisibleRect;
    if (rect.origin.y > 6000) {
            rect.origin.y = 4000;
            [self.contentView setBounds:rect];
    }
}

reflectScrolledClipView 似乎与scrollToPoint 发生冲突,并且与 [self.contentView setBounds:rect]; 一起使用时导致堆栈溢出。滚动的方法。

关于macos - NSScrollView 并在滚动时手动设置内容偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25791989/

相关文章:

macos - 相当于 NSCollectionView 的 ReloadData 来重绘项目 View

objective-c - Hook C 函数

macos - SMJobBless 用于特权助手工具,但是如果我想将 launchd plist 复制到/Users/<user>/Library/LaunchAgents 该怎么办

c# - 我正在尝试使用NAudio创建效果Delay

android - 如何在 imageSwitcher 图像更改时设置延迟 (Android)

node.js - ElectronJS 应用程序图标未出现在 Apple App Store Connect 中

macos - GVM使用隐藏目录

macos - NSImageView 更改事件处理程序

objective-c - 在 Cocoa WebView 中防止 Flash

google-chrome - 如何使用 Chrome Headless 模式延迟捕获 DOM