Question

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?

Was it helpful?

Solution

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

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

OTHER TIPS

I wanted to do the same thing you are trying to do Ben, but I found that the animation was too costly in terms of display time and seemed to defeat the purpose. I played around with it and found the following code does exactly what you wanted it to do and doesn't bog down the system at all.

Setup

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

The key is in turning the animation OFF and moving the offset in smaller increments. It seems to run faster and it doesn't interfere with the manual 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 pretty new to objective-c and iphone development so if anybody with more experience sees a problem with this approach I would appreciate any feedback.

I think by having effectively 2 ways to scroll the content your kind of fighting the API.

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

When you scroll in this way, an animation is created and run on another thread. While the animation is running, the presentation layer is being changed. However, the actual content offset is NOT set until the animation completes.

This works fine and transparently, until you attempt to animate the same property while it is already being animated.

Normally, in this case you cancel the existing animations, set the frame to the presentation layer (current onscreen position), then apply the new animation.

However, you are using a convenience method of the textView (setContentOffset:animated:). When you do this you give up fine animation control, in exchange for less lines of code.

I think that stopping a scroll in progress may be hard, or impossible using the higher level APIs. This means to do what you want you would at least have to try a UIView animation block and most likely actually create a CAAnimation.

The easier solution is to settle on one means of scrolling your textView. The HIG favors using direct manipulation on the iPhone. Even if it is easy to perform the scroll both ways, I would recommend ditching the button method, and just use the direct manipulation you are now implementing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top