如何在Xcode中创建自动滚动UITextView

时间:2014-07-25 07:55:48

标签: ios iphone

我按下按钮启动时尝试创建一个带自动滚动功能的UITexView。并按下按钮,按下它将停止滚动。再次按下按钮,它将继续向下滚动。如何处理这个功能。 我研究了一些代码,下面的代码与我合作。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"%f %f",_txvSentence.contentSize.width , _txvSentence.contentSize.height);
    if (scrollingTimer == nil) {
        scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:(35.0/1000.0)
                                                          target:self
                                                        selector:@selector(autoscrollTimerFired:)
                                                        userInfo:nil
                                                         repeats:YES];

        NSLog(@"%@",scrollingTimer);
    }
}
- (void)autoscrollTimerFired:(NSTimer*)timer {
    CGPoint scrollPoint = self.txvSentence.contentOffset;
    //scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 1);

    scrollPoint = _txvSentence.contentOffset;
    scrollPoint.y= scrollPoint.y+1;
    [_txvSentence setContentOffset:scrollPoint animated:NO];

    //[self.txvSentence setContentOffset:scrollPoint animated:NO];
}

但我不能停止这个动画。以及如何设置动态持续时间,例如文本结束时自动停止。

抱歉,因为我的英语不好。如果不明白,请告诉我将在哪里清楚解释。

2 个答案:

答案 0 :(得分:0)

试试这个:

self.scrollView.scrollEnabled = NO;

CGFloat scrollHeight = 100;


//===

- (void) spinWithOptions: (UIViewAnimationOptions) options {
    // this spin completes 360 degrees every 2 seconds
    [UIView animateWithDuration:10
                          delay:0
                        options:options
                     animations:^{
                         self.scrollView.contentOffset = CGPointMake(0, scrollHeight);
                     }
                     completion:^(BOOL finished) {
                         if (finished) {
                             if (animating) {
                                 // if flag still set, keep spinning with constant speed
                                 [self spinWithOptions: UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction];
                             } else if (options != UIViewAnimationOptionCurveEaseOut) {
                                 // one last spin, with deceleration
                                 [self spinWithOptions: UIViewAnimationOptionCurveEaseOut];
                             }
                         }

                     }];
}


- (void) startAnimating {
    if (!animating) {
        animating = YES;
        [self spinWithOptions: UIViewAnimationOptionCurveLinear |      UIViewAnimationOptionAllowUserInteraction];
    }
}

- (void) stopAnimating {
    // set the flag to stop spinning after one last 90 degree increment
    animating = NO;
}

答案 1 :(得分:0)

您可以使用NSTimer。

// Global var
CGFloat scrollHeight = MAX_HEIGHT;
CGFloat currentOffsetY = 0.0f;
NSTimer *timer;

self.timer = [NSTimer scheduledTimerWithTimeInterval:YOUR_TIME_INTERVAL target:self selector:@selector(scrollText:) userInfo:nil repeats:YES];

- (void)scrollText:(NSTimer *)timer {
    currentOffsetY++;
    if (currentOffsetY >= MAX_HEIGHT) {
        [self.timer invalide];
    }
    self.scrollView.contentOffset = CGPointMake(0, currentOffsetY);
}

// Your button action
- (void)stopAnimation:(UIButton *)button {
    [self.timer invalide];
}