如何在UITextView中启用平滑滚动?

时间:2011-09-08 14:41:15

标签: iphone objective-c uitextview

I've got my UITextView set up for scrolling like this,

-(void)startAutoScroll
{
    NSLog(@"AutoScroll Started");
    if (scrollingTimer == nil) {
        scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:(60.0/1000.0)
                                                          target:self
                                                        selector:@selector(autoscrollTimerFired:) 
                                                        userInfo:nil 
                                                         repeats:YES];
    }

}

- (void) autoscrollTimerFired:(NSTimer *)timer
{
    scrollPoint = self.completeText.contentOffset;
    scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + velocityFactor);
    [self.completeText setContentOffset:scrollPoint animated:NO];
}

如何启用平滑滚动?感谢

1 个答案:

答案 0 :(得分:1)

您可以使用动画NO逐个像素滚动,因为它在完成滚动后不像动画YES属性那样停止。你要设置的唯一东西是velocityFactor作为你应该调用NSTimer的时间,而不是滚动应该移动。在使用contentSize完成滚动后,使计时器无效并且滚动应该停止。

- (void) autoscrollTimerFired:(NSTimer *)timer {
    [self.completeText setContentOffset:CGPointMake(0, self.completeText.contentOffset.y + 1.0) animated:NO];
    if (self.completeText.contentOffset.y != self.completeText.contentSize.height - self.completeText.frame.size.height) {
        scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:velocityFactor target:self selector:@selector(autoscrollTimerFired:) userInfo:nil repeats:NO];
    } else {
        [scrollingTimer invalidate];
    }
}