IPhone Auto-scroll UITextView, but also allows manual scrolling

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

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

This works very well as the animation makes the scrolling pretty smooth. I want to allow the user to scroll forward or backward by scrolling with the finger, however, because of this code, after the scroll animation, the scrolling returns back to where it will automatically scroll.

I need to reset the scrollPoint variable after manually scrolling, but I'm not sure how to do this. I tried to implement the delegate method

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

but this method also works in my auto scroll.

Any ideas?

+5
source share
3 answers

You can simply move it relative to where it is:

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

I wanted to do the same thing that you are trying to do Ben, but I found that the animation was too expensive in terms of show time and seemed to beat the target. I played with him and found that the following code does exactly what you wanted it to do and not shut down the system at all.

Customization

 if (autoscrollTimer == nil) { autoscrollTimer = [NSTimer scheduledTimerWithTimeInterval:(35.0/1000.0) target:self selector:@selector(autoscrollTimerFired:) userInfo:nil repeats:YES]; } 

The key is to turn off the animation and move the offset in smaller increments. It seems to work faster and does not interfere with the hand scroll.

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

I am new to developing objective-c and iphone, so if someone with a lot of experience sees a problem with this approach, I would appreciate any feedback.

+2
source

I think you have 2 ways to scroll through the content in which you are fighting the API.

If I understand correctly, in your current setup you have a button that starts the animation.

When you scroll this way, the animation is created and launched in another thread. During the animation, the presentation layer changes. However, the actual content offset is NOT until the animation completes.

This works fine and transparently, until when you try to animate the same property when it is already animated.

Usually in this case, you cancel the existing animations, set the frame to the presentation level (current screen position), and then apply a new animation.

However, you use the convenient method textView (setContentOffset: animated :). When you do this, you give up fine animation control in exchange for fewer lines of code.

I think that stopping scrolling can be difficult or impossible using a higher level API. This means doing what you want, at least you need to try the UIView animation block and, most likely, create a CAAnimation.

A simpler solution is to install one text scroll tool. HIG supports the use of direct manipulation on the iPhone. Even if you can easily scroll in both directions, I would recommend using the button method and just use the direct manipulation that you are currently implementing.

0
source

Source: https://habr.com/ru/post/896380/


All Articles