【问题标题】:How can I programmatically scroll UIScrollView from start to end slowly如何以编程方式从头到尾缓慢滚动 UIScrollView
【发布时间】:2011-07-28 11:09:57
【问题描述】:

我有一个显示大约 10 个图像的水平 UIScrollview。 我知道我们必须使用 scrollRectToVisible 方法以编程方式移动滚动视图。 但我正在寻找的是从滚动视图的开始到结束缓慢滚动滚动视图(1 秒内 5 个像素)。

我看过一些页面,但我不明白如何在我的代码中集成以下代码:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:(abs(rMid-pMid)*0.3)];
scrollMid.contentOffset = CGPointMake(rMid*320, 0);
[UIView commitAnimations];

【问题讨论】:

    标签: ios uiscrollview


    【解决方案1】:

    你可以像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];
    }
    

    希望对你有所帮助...

    【讨论】:

    • 我个人对此没有任何特殊用途,但我对其进行了测试,它就像一个魅力!
    • 嗨,这段代码真的有效。但是滚动不流畅。即使将动画更改为YES,滚动也不流畅。我怎样才能实现该功能?
    • 滚动取决于间隔时间和Y的增量
    猜你喜欢
    • 2011-01-15
    • 2010-10-31
    • 2013-04-17
    • 2015-08-28
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    相关资源
    最近更新 更多