Making uitextview scroll programmatically

前端 未结 2 1981
青春惊慌失措
青春惊慌失措 2020-12-19 09:50

I want to make my uitextview to scroll automatically whenever the application is launched. Can anyone help me with a detailed code? I am new to iPhone SDK.

2条回答
  •  春和景丽
    2020-12-19 10:18

    UITextView derives from UIScrollview so you can set the scrolling position using -setContentOffset:animated:.

    Assuming you want to scroll smoothly at the speed of 10 points per second, you'd do something like that.

    - (void) scrollStepAnimated:(NSTimer *)timer {
        CGFloat scrollingSpeed = 10.0; // 10 points per second
        NSTimeInterval repeatInterval = [timer timeInterval]; // ideally, something like 1/30 or 1/10 for a smooth animation
    
        CGPoint newContentOffset = CGPointMake(self.textView.contentOffset.x, self.textView.contentOffset.y + scrollingSpeed * repeatInterval);
        [self.textView setContentOffset:newContentOffset animated:YES];
    }
    

    Of course you have to setup the timer and be sure to cancel the scrolling when the view disappears and so on.

提交回复
热议问题