【问题标题】:Programmatically scrolling UIScrollView then over-riding with manual scroll以编程方式滚动 UIScrollView 然后用手动滚动覆盖
【发布时间】:2014-02-12 22:46:10
【问题描述】:

我希望滚动视图在用户第一次加载屏幕时自动滚动...所以我使用以下代码:

[UIView animateWithDuration:(float)1.25f
                     animations:^{
                         myScrollView.contentOffset = CGPointMake(2000, 800);
                     }
                     completion:nil];

所以代码工作得很好,UIScrollView 模拟了一个“滚动”(动画)到我想要的 CGPoint,但是如果用户想要手动将手指放在它上面并开始滚动或停止它,它正在滚动,用户在完成之前无法覆盖此动画。

任何人都知道一种更好的方法来使用覆盖功能对其进行动画处理,以及何时调用所述覆盖功能(我假设这将涉及到 scrollView 视图上的 touchesBegan)

【问题讨论】:

    标签: ios objective-c animation uiscrollview overriding


    【解决方案1】:

    只需将UIViewAnimationOptionAllowUserInteraction 添加到您的动画选项中:

    [UIView animateWithDuration:1.25f delay:0.0f options:(UIViewAnimationOptionAllowUserInteraction) animations:^{
                         _scrollView.contentOffset = CGPointMake(0, 800);
                     }
                     completion:nil];
    

    并取消UIScrollView上的动画scrollViewWillBeginDragging:委托方法

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
        //Storing current offset
        CALayer *currentLayer = _scrollView.layer.presentationLayer;
        CGPoint scrollBoundsOrigin = currentLayer.bounds.origin;
    
        //Cancelling animations
        [_scrollView.layer removeAllAnimations];
    
        //Restore offset
        _scrollView.contentOffset = scrollBoundsOrigin;
    }
    

    【讨论】:

    • 太棒了!非常感谢!像魅力一样工作:)
    猜你喜欢
    • 2011-01-15
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    相关资源
    最近更新 更多