Создание прокрутки uitextview программно - PullRequest
6 голосов
/ 01 сентября 2011

Я хочу, чтобы мой uitextview автоматически прокручивался при каждом запуске приложения. Может кто-нибудь помочь мне с подробным кодом? Я новичок в iPhone SDK.

Ответы [ 2 ]

11 голосов
/ 01 сентября 2011

.h файл

@interface Credits : UIViewController 
{
    NSTimer *scrollingTimer;

    IBOutlet UITextView *textView;


}
@property (nonatomic , retain) IBOutlet UITextView *textView;

- (IBAction) buttonClicked ;

- (void) autoscrollTimerFired;

@end

.m файл

- (void) viewDidLoad
{       
    // it prints the initial position of text view 
    NSLog(@"%f %f",textView.contentSize.width , textView.contentSize.height);

    if (scrollingTimer == nil)
    {
        // A timer that updates the content off set after some time so it can scroll 
        // you can change time interval according to your need (0.06)
        // autoscrollTimerFired is the method that will be called after specified time interval. This method will change the content off set of text view
        scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:(0.06)
                         target:self selector:@selector(autoscrollTimerFired) userInfo:nil repeats:YES];        
    }
}

- (void) autoscrollTimerFired
{
    CGPoint scrollPoint = self.textView.contentOffset; // initial and after update
    NSLog(@"%.2f %.2f",scrollPoint.x,scrollPoint.y);
    if (scrollPoint.y == 583) // to stop at specific position 
    {
        [scrollingTimer invalidate];
        scrollingTimer = nil;
    }
    scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 1); // makes scroll
    [self.textView setContentOffset:scrollPoint animated:NO];
    NSLog(@"%f %f",textView.contentSize.width , textView.contentSize.height);

}

Надеюсь, это поможет вам ....

1 голос
/ 01 сентября 2011

UITextView является производным от UIScrollview, поэтому вы можете установить позицию прокрутки, используя -setContentOffset: animated:.

Предполагая, что вы хотите плавно прокручиваться со скоростью 10 точек в секунду, вы сделаете что-то подобное.

- (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];
}

Конечно, вам нужно настроить таймер и обязательно отменить прокрутку, когда вид исчезнет, ​​и так далее.

...