NSTextView, добавление текста и плавная прокрутка - PullRequest
12 голосов
/ 02 февраля 2012

Я пытаюсь реализовать плавную прокрутку в представлении истории чата, которое я реализовал, однако, если добавляемый контент достаточно большой, плавная прокрутка будет прокручиваться только на несколько строк.

Мое первое предположение состояло в том, что представление еще не перерисовалось само по себе ... не так, даже если принудительное немедленное рисование с -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];
    }
}

Что я делаю не так?

1 Ответ

2 голосов
/ 07 ноября 2012

Это может помочь вам ...

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

}

...