2012-02-02 19 views
12

Estoy tratando de implementar un desplazamiento suave en una vista del historial de chat que implementé; sin embargo, si el contenido que añado es lo suficientemente grande, el desplazamiento suave solo se desplazará por unas pocas líneas.NSTextView, anexando texto y desplazamiento suave

Mi primera suposición fue que la vista no se volvió a dibujar todavía ... no es el caso, incluso cuando se fuerza el dibujo inmediato con la pantalla todavía se rompe.

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

¿Qué estoy haciendo mal?

+0

Estoy seguro de que esto no va a resolver su problema, pero en el código no animada de su rango a estar fuera de los límites, ya que terminará en la longitud + 1. – mattmook

+0

Utilicé ese código por un tiempo, probablemente tendría que buscar en los documentos por qué la (ubicación + 1) del último carácter (ya que la longitud es (ubicación + 1) supongo) está siendo aceptada. –

Respuesta

2

Esto puede ayudarle a ...

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

}

Cuestiones relacionadas