NSTextView,附加文本和平滑滚动

发布于 2025-01-01 18:48:12 字数 830 浏览 1 评论 0原文

我正在尝试在我实现的聊天历史记录视图中实现平滑滚动,但是如果我附加的内容足够大,平滑滚动只会滚动几行。

我的第一个猜测是视图还没有重新绘制本身......事实并非如此,即使使用 -display 强制立即绘制它仍然会中断。

- (void)scrollAnimated:(BOOL)animated
{
    if( animated )
    {
        NSClipView *clipView = [[_chatHistoryView enclosingScrollView] contentView];

        [NSAnimationContext beginGrouping];
        [[NSAnimationContext currentContext] setDuration:0.100f];
        NSPoint constrainedPoint = [clipView constrainScrollPoint:NSMakePoint(0, CGFLOAT_MAX)];
        [[clipView animator] setBoundsOrigin:constrainedPoint];
        [NSAnimationContext endGrouping];
    }
    else
    {
        NSRange range;
        range.location = [[_chatHistoryView textStorage] length];
        range.length = 1;
        [_chatHistoryView scrollRangeToVisible:range];
    }
}

我做错了什么?

I am trying to implement smooth scrolling in a chat history view I implemented, however if the content I append is big enough the smooth scroll will only scroll for a few lines.

My first guess was that the view did not redraw itself yet.. not the case, even when forcing immediate drawing with -display it still breaks.

- (void)scrollAnimated:(BOOL)animated
{
    if( animated )
    {
        NSClipView *clipView = [[_chatHistoryView enclosingScrollView] contentView];

        [NSAnimationContext beginGrouping];
        [[NSAnimationContext currentContext] setDuration:0.100f];
        NSPoint constrainedPoint = [clipView constrainScrollPoint:NSMakePoint(0, CGFLOAT_MAX)];
        [[clipView animator] setBoundsOrigin:constrainedPoint];
        [NSAnimationContext endGrouping];
    }
    else
    {
        NSRange range;
        range.location = [[_chatHistoryView textStorage] length];
        range.length = 1;
        [_chatHistoryView scrollRangeToVisible:range];
    }
}

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£冰雨忧蓝° 2025-01-08 18:48:12

这可能会帮助你...

- (void)maybeAutoscrollForThumb:(ThumbImageView *)thumb {

autoscrollDistance = 0;

// only autoscroll if the thumb is overlapping the thumbScrollView
if (CGRectIntersectsRect([thumb frame], [thumbScrollView bounds])) {

    CGPoint touchLocation = [thumb convertPoint:[thumb touchLocation] toView:thumbScrollView];
    float distanceFromLeftEdge  = touchLocation.x - CGRectGetMinX([thumbScrollView bounds]);
    float distanceFromRightEdge = CGRectGetMaxX([thumbScrollView bounds]) - touchLocation.x;

    if (distanceFromLeftEdge < AUTOSCROLL_THRESHOLD) {
        autoscrollDistance = [self autoscrollDistanceForProximityToEdge:distanceFromLeftEdge] * -1; // if scrolling left, distance is negative
    } else if (distanceFromRightEdge < AUTOSCROLL_THRESHOLD) {
        autoscrollDistance = [self autoscrollDistanceForProximityToEdge:distanceFromRightEdge];
    }        
}

// if no autoscrolling, stop and clear timer
if (autoscrollDistance == 0) {
    [autoscrollTimer invalidate];
    autoscrollTimer = nil;
} 

// otherwise create and start timer (if we don't already have a timer going)
else if (autoscrollTimer == nil) {
    autoscrollTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / 60.0)
                                                       target:self 
                                                     selector:@selector(autoscrollTimerFired:) 
                                                     userInfo:thumb 
                                                      repeats:YES];
} 

}

This may help you...

- (void)maybeAutoscrollForThumb:(ThumbImageView *)thumb {

autoscrollDistance = 0;

// only autoscroll if the thumb is overlapping the thumbScrollView
if (CGRectIntersectsRect([thumb frame], [thumbScrollView bounds])) {

    CGPoint touchLocation = [thumb convertPoint:[thumb touchLocation] toView:thumbScrollView];
    float distanceFromLeftEdge  = touchLocation.x - CGRectGetMinX([thumbScrollView bounds]);
    float distanceFromRightEdge = CGRectGetMaxX([thumbScrollView bounds]) - touchLocation.x;

    if (distanceFromLeftEdge < AUTOSCROLL_THRESHOLD) {
        autoscrollDistance = [self autoscrollDistanceForProximityToEdge:distanceFromLeftEdge] * -1; // if scrolling left, distance is negative
    } else if (distanceFromRightEdge < AUTOSCROLL_THRESHOLD) {
        autoscrollDistance = [self autoscrollDistanceForProximityToEdge:distanceFromRightEdge];
    }        
}

// if no autoscrolling, stop and clear timer
if (autoscrollDistance == 0) {
    [autoscrollTimer invalidate];
    autoscrollTimer = nil;
} 

// otherwise create and start timer (if we don't already have a timer going)
else if (autoscrollTimer == nil) {
    autoscrollTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / 60.0)
                                                       target:self 
                                                     selector:@selector(autoscrollTimerFired:) 
                                                     userInfo:thumb 
                                                      repeats:YES];
} 

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文