Iphone 自动滚动UITextView问题

Iphone 自动滚动UITextView问题,iphone,cocoa-touch,uikit,Iphone,Cocoa Touch,Uikit,我正在尝试自动滚动我的文本视图,并在它到达末尾时将其重置为顶部 我使用以下代码: -(void)scrollTextView { CGPoint scrollPoint = stationInfo.contentOffset; scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 2); if (scrollPoint.y == originalPoint.y + 100) { N

我正在尝试自动滚动我的文本视图,并在它到达末尾时将其重置为顶部

我使用以下代码:

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

}
结果,文本视图疯狂地跳来跳去,但我不知道为什么。是否有更好的方法检测文本视图位于底部?我是否将
滚动点
值设置错误

编辑:

问题解决了!我坚持使用NSTimer-缺少的键正在调用层的显示

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


}

您还可以使用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%确定,但这就是它应该如何工作。

重置会造成混乱,这就是为什么我问如何正确重置它的原因。:/解决方法是不使用计时器,而是使用CoreAnimation。完成后,您会收到一个回调,在此过程中,您将内容偏移量设置为(0,0)。我很高兴保留计时器,它会检测到结束状态,但如果我将偏移量重置为0,0,它会再次跳转。抱歉,此代码格式不正确,我无法识别是什么。同时,我已经成功地开发了自己的解决方案,但是老问题又出现了,即使我将偏移量设置为0,0,它也不会重置。我知道它何时到达底部,所以我停止计时器并设置偏移量,但它不会重置。代码的格式似乎不好,但它是这样的:第一个函数调用“animateWithDuration”的animations参数以一个块作为参数。在第一行中,此块是用^{然后在块内设置contentOffset。然后关闭块,最后一个参数完成:获取动画结束时将执行的另一个块。该块获取一个参数,如果已完成,则为BOOL,如果动画已完成,则重置contentOffset。回调方法具有错误的签名。L看看我的答案,看看正确的答案。这段代码很有效,这很好。请注意,这是一段非常低级的代码。CoreAnimation会让它变得更简单。我承认阅读文档需要一些时间,但这是值得的。有一个问题仍然存在:为什么您直接与CALayer交互?因为您没有直接处理层(或者你)我会打电话给[stationInfo setNeedsDisplay]。为什么要设置内容偏移动画?您正在使用计时器设置动画,因此我不会为每个2像素的步长使用动画。真正的问题可能是,您在那里触发的隐式动画比您的0.1时间步长长,这会把事情搞得一团糟。向下滚动时,请尝试在没有动画的情况下设置动画。也许你在CALayer上的显示调用也可以省略,这对我来说仍然很奇怪。我已将其更改为
[stationInfo setNeedsDisplay];
,但我保留了其余部分-它完全按照我的要求工作。内存占用也很低,所以对我来说没有问题。正如我所说的:只要它工作,就保留它。