Auto Scrolling UITextView Problem

2019-10-29 01:32发布

我想自动滚动我的文本视图,一旦它在抵达终点其重置到顶部。

我用这个代码:

-(void)scrollTextView
{

    CGPoint scrollPoint = stationInfo.contentOffset; 

    scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 2);

    if (scrollPoint.y == originalPoint.y + 100)
    {
        NSLog(@"Reset it");

        scrollPoint = CGPointMake(originalPoint.x, originalPoint.y);
        [stationInfo setContentOffset:scrollPoint animated:YES];

        [scroller invalidate];
        scroller = nil;

        scroller = [NSTimer
                    scheduledTimerWithTimeInterval:0.1
                    target:self
                    selector:@selector(scrollTextView)
                    userInfo:nil
                    repeats:YES];

    }
    else
    {
        [stationInfo setContentOffset:scrollPoint animated:YES];
    }

}

其结果是,文中观点跳跃各地疯狂,但我不太知道为什么。 有可能的检测文本视图是在底部更好的办法? 我是不是设置了scrollPoint值错了吗?

编辑:

问题是否已解决! 我坚持的NSTimer -丢失的钥匙在呼唤-display的层。

    -(void)scrollTextView
    {
        //incrementing the original point to get movement
        originalPoint = CGPointMake(0, originalPoint.y + 2);
        //getting the bottom
        CGPoint bottom = CGPointMake(0, [stationInfo contentSize].height);
        //comparing the two to detect a reset
        if (CGPointEqualToPoint(originalPoint,bottom) == YES) 
        {
            NSLog(@"Reset");
            //killing the timer
            [scroller invalidate];
            scroller == nil;
            //setting the reset point
            CGPoint resetPoint = CGPointMake(0, 0);
            //reset original point
            originalPoint = CGPointMake(0, 0);
            //reset the view.
            [stationInfo setContentOffset:resetPoint animated:YES];
            //force display
            [stationInfo.layer display];

            scroller = [NSTimer
                        scheduledTimerWithTimeInterval:0.1
                        target:self
                        selector:@selector(scrollTextView)
                        userInfo:nil
                        repeats:YES];
        }
        else
        {   
            [stationInfo setContentOffset:originalPoint animated:YES];
        }


}

Answer 1:

你也可以使用CoreAnimation和直接动画bounds属性。 首先动画滚动,然后在动画已完成重置内容偏移委托回调。

回调方法必须有签名

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context

你也可以使用新的基于块的方法,如果你的目标的iOS 4.0及以上。 再就是要传递两大块:在第一个你指定什么动画,并在第二次时的动画效果是在做什么。

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

你的问题不是塌陷成一个单一的代码行:

[stationInfo animateWithDuration:10.f delay:0.f options:0 animations:^{
    [stationInfo setContentOffset:CGPointMake(0, [stationInfo contentSize].height)];
} completion:^(BOOL finished){
    if (finished) [stationInfo setContentOffset:CGPointMake(0,0)];
}];

说实话,我不是100%肯定的精确块语法,但是这是它应该如何工作。



文章来源: Auto Scrolling UITextView Problem