2012-02-02 35 views
12

Tôi đang cố gắng thực hiện cuộn trơn tru trong chế độ xem lịch sử trò chuyện mà tôi đã triển khai, tuy nhiên nếu nội dung tôi thêm đủ lớn thì cuộn trơn sẽ chỉ cuộn cho một vài dòng.NSTextView, thêm văn bản và cuộn trơn tru

phỏng đoán đầu tiên của tôi là chế độ xem không tự vẽ lại .. không phải trường hợp, ngay cả khi bắt buộc vẽ ngay lập tức bằng cách hiển thị nó vẫn bị ngắt.

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

Tôi đang làm gì sai?

+0

Tôi chắc chắn điều này sẽ không giải quyết vấn đề của bạn, nhưng trong các mã không động phạm vi của bạn sẽ nằm ngoài giới hạn vì nó sẽ kết thúc ở độ dài + 1. – mattmook

+0

Được sử dụng mã đó trong một thời gian, tôi có lẽ sẽ phải tìm trong tài liệu tại sao (vị trí +1) của char cuối cùng (vì độ dài là (vị trí +1) tôi đoán) đang được chấp nhận. –

Trả lời

2

này có thể giúp bạn ...

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

}

Các vấn đề liên quan