如何以编程方式缓慢地将UIScrollView从开始滚动到结束

4
我有一个横向的UIScrollview,显示大约10张图片。 我知道我们必须使用scrollRectToVisible方法来编程地移动ScrollView。 但是我想要的是从开始到结束缓慢滚动ScrollView (每秒5个像素)。 我看过一些页面,但我不知道如何将下面的代码集成到我的代码中:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:(abs(rMid-pMid)*0.3)];
scrollMid.contentOffset = CGPointMake(rMid*320, 0);
[UIView commitAnimations];
1个回答

5

你可以像下面这样使用NSTimer

- (void) viewDidLoad
{       
    if (scrollingTimer == nil)
    {
        scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:(0.06)
                         target:self selector:@selector(autoscrollTimerFired) userInfo:nil repeats:YES];        
    }
}

- (void) autoscrollTimerFired
{
    if (scrollPoint.y == 583) // at where you want to stop scroll
    {
        [scrollingTimer invalidate];
        scrollingTimer = nil;
    }
    scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 1);
    [self.scrollView setContentOffset:scrollPoint animated:NO];
}

希望本文对您有所帮助...


1
我个人没有特别的用途,但我测试过它,它非常好用! - MiguelB
嗨,这段代码确实可以工作。但是滚动不够平滑。即使将"animated"更改为YES,滚动也不够平滑。我该如何实现这种功能? - Satyam
滚动取决于时间间隔和Y轴的增量。 - Maulik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接