iPhone自动滚动UITextView,但也允许手动滚动(iPhone Auto-scroll UITextView but allow manual scrolling also)

245 IT屋

I have a UITextView that has a lot of content. I have a button that allows the UITextView to automatically scroll + 10 pixels in an NSTimer loop:

scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 10);
[textView setContentOffset:scrollPoint animated:YES];   

This works really well, as the animation makes the scroll rather smooth. I want to allow the user to skip ahead or back by scrolling with their finger, however due to this code after the scroll animation, the scroll snaps back to where it would have auto-scrolled.

I need to reset the scrollPoint variable after a manual scroll, but I'm not sure how to do that. I've tried implementing the delegate method

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;

but this method fires on my automatic scroll as well.

Any ideas?

解决方案

You could just make it move relative to where it is:

scrollPoint = textView.contentOffset;
scrollPoint.y= scrollPoint.y+10;
[textView setContentOffset:scrollPoint animated:YES];

我有一个包含大量内容的UITextView。我有一个按钮,允许UITextView在NSTimer循环中自动滚动+10像素:



  scrollPoint = CGPointMake(scrollPoint.x ,scrollPoint.y + 10); 
[textView setContentOffset:scrollPoint animated:YES];


这很有效,因为动画使滚动相当平滑。我希望允许用户通过用手指滚动来向前或向后跳过,但是由于滚动动画后的代码,滚动会快速回到自动滚动的位置。



我需要在手动滚动后重置scrollPoint变量,但我不知道该怎么做。我已经尝试实现委托方法



   - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView; 


但这种方法也会触发我的自动滚动。



任何想法?


解决方案

你可以让它相对于它的位置移动:



  scrollPoint = textView.contentOffset; 
scrollPoint.y = scrollPoint.y + 10;
[textView setContentOffset:scrollPoint animated:YES];

本文地址:IT屋 » iPhone自动滚动UITextView,但也允许手动滚动