2012-02-02 32 views
12

我试图在我实现的聊天历史视图中实现平滑滚动,但是如果我追加的内容足够大,则平滑滚动只会滚动几行。NSTextView,附加文本和平滑滚动

我的第一个猜测是,该视图并没有重新绘制自己..不是这样,即使强制即时绘图使用-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]; 
    } 
} 

我在做什么错?

+0

我敢肯定,这不会解决你的问题,但在非动画代码的范围会出界,因为它将以+ 1结束。 – mattmook 2012-10-29 01:34:33

+0

在使用该代码一段时间后,我可能不得不查看文档为什么最后一个字符的位置(1)(因为长度是(我猜的位置+ 1))被接受。 – 2012-10-29 11:52:53

回答

2

这可能会帮助你...

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

}