iPhone Auto-scroll UITextView but allow manual scrolling also

前端 未结 3 813
别跟我提以往
别跟我提以往 2020-12-16 05:58

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:

scrollPoin         


        
相关标签:
3条回答
  • 2020-12-16 06:40

    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.

    0 讨论(0)
  • 2020-12-16 06:46

    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.

    0 讨论(0)
  • 2020-12-16 06:47

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

    scrollPoint = textView.contentOffset;
    scrollPoint.y= scrollPoint.y+10;
    [textView setContentOffset:scrollPoint animated:YES];
    
    0 讨论(0)
提交回复
热议问题