简体   繁体   中英

How to enable smooth scrolling in 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];
}

How to enable smooth scrolling? thanks

You can make the trick scrolling pixel by pixel with animated NO since it doesn't stop like the animated YES property after its done scrolling. The only thing you gotta set is the velocityFactor as the time your NSTimer should be called, not the scroll should move. And after the scroll is done with the contentSize, invalidate the timer and the scroll should stop.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM