【问题标题】:UIScrollview setContentOffset with non linear animation?UIScrollview setContentOffset 与非线性动画?
【发布时间】:2012-06-13 19:17:09
【问题描述】:

当您实际滚动到下一页时,我正在尝试重现启用分页的滚动视图的流畅动画。似乎是UIViewAnimationCurveEaseInOut,但我需要有一个“下一页”按钮并以编程方式触发滚动。

这是我的代码:

-(void) scrollToPage:(int)page
{
    UIScrollView *scrollView = contentView;
    CGPoint offset = CGPointMake(scrollView.bounds.size.width * page, scrollView.contentOffset.y);
    [scrollView setContentOffset:offset animated: YES];     
    [self pageControlUpdate];
}

-(void) scrollToNextPage 
{
    [self scrollToPage:(pageControl.currentPage + 1)];
}

我无法重现UIViewAnimationCurveEaseInOut的流畅度, 或者setContentOffset,或者scrollRectToVisible... 它以丑陋的线性动画进入下一页

我什至尝试手动对其进行动画处理:

[UIView animateWithDuration:.5 delay:0 options:UIViewAnimationCurveEaseInOut animations:^{
    scrollView.contentOffset = offset;
} completion:^(BOOL finished) {    } ];

我哪里错了?

【问题讨论】:

  • 我发现了这个:developer.apple.com/library/ios/#DOCUMENTATION/WindowsViews/…,这证实了我所看到的:The setContentOffset:animated: method scrolls the content to the specified content offset. If the animated parameter is YES, the scrolling will animate from the current position to the specified position at a constant rate.
  • 参数。没人知道吗?
  • 好吧,如果UIViewAnimationCurveEaseInOut 不起作用,它可能是不同的动画曲线。 Apple 很可能会为此动画使用自定义曲线。使用CAPropertyAnimation,您可以以CAMediaTimingFunction 的形式定义自己的自定义曲线。可能值得尝试使用不同的控制点。
  • 我无法让它工作。你知道使用 CAPropertyAnimation 的教程或代码示例吗?

标签: iphone ios ipad uiscrollview


【解决方案1】:

每次制作自己的动画时,都必须将 NO 作为animated: 参数传递:

- (void)scrollToPage:(int)page
{
    UIScrollView *scrollView = contentView;
    CGPoint offset = CGPointMake(scrollView.bounds.size.width * page, 
                                 scrollView.contentOffset.y);

    [UIView animateWithDuration:.5
                          delay:0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         [scrollView setContentOffset:offset animated:NO];
                     } completion:nil];

    [self pageControlUpdate];
}

【讨论】:

  • 顺便说一句。在某些情况下,修复动画时间看起来很奇怪。例如你只滚动 2 行它也需要半秒。而另一个方向,如果你有一个非常非常长的列表并且你从上到下滚动它会在半秒内超级快。
  • 感谢您的解释,但我只滚动了一个完整的页面宽度,所以它看起来总是相同的速度。我尝试使用动画:NO 参数,但它一直以固定速率滚动。我还尝试更改 UIViewAnimationCurve 动画,但它看起来一样。我正在尝试 Ole 的自定义 CAPropertyAnimation 解决方案。
  • 这很好用。小挑剔,它应该是 UIViewAnimationOptionCurveEaseInOut。
  • 很好的答案,最好在这里!
【解决方案2】:

使用公共 API,我认为这目前是不可能的。假设您在动画过程中不需要用户交互,您最好将UIScrollView 的子视图(即滚动视图的内容)的位置设置为动画,然后调整contentOffset 而不动画完成。你可以这样做:

- (void) scrollToPage:(int)page {
    UIScrollView *scrollView = contentView;
    scrollView.userInteractionEnabled = NO;
    CGPoint offset = CGPointMake(scrollView.bounds.size.width * page, scrollView.contentOffset.y);
    CGFloat delta = offset.x - scrollView.contentOffset.x;

    __block int animationCount = 0;
    for (UIView *view in scrollView.subviews) {
        [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            animationCount++;
            CGRect frame = view.frame;
            frame.origin.x -= delta;
            view.frame = frame;
        } completion:^(BOOL finished) {
            animationCount--;
            if (animationCount == 0) {
                scrollView.contentOffset = offset;
                for (UIView *view in scrollView.subviews) {
                    CGRect frame = view.frame;
                    frame.origin.x += delta;
                    view.frame = frame;
                }
                scrollView.userInteractionEnabled = YES;
            }
        }];
    }
}

我可以确认它按预期工作,我自己测试过。

【讨论】:

  • 是的,这是一个有趣的替代解决方案。但我确实需要使用实时 contentOffset 值在滚动时将我的视图移动到滚动视图内,并且此解决方案不会触发 scrollviewdidscroll。
  • 当您在末尾设置contentOffset 时,它确实会触发scrollViewDidScroll。但是您可以只提取您在 scrollViewDidScroll 中执行的任何额外处理,然后在此方法中使用它。
  • 另外,您可以将所有UIScrollView 子视图嵌入到另一个UIView 中,您可以子类化并覆盖setFrame:,因为在动画期间它基本上等同于scrollViewDidScroll。
【解决方案3】:
[UIView animateWithDuration:(Animation_Duration)
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                     [scroll setContentOffset:CGPointMake(PointX, PointY) animated:NO];
                      }
                     completion:^(BOOL finished){}];`

【讨论】:

    【解决方案4】:

    这门课绝对救了我的命:

    MOScroll on GitHub.com

    有

    - (void)setContentOffset:(CGPoint)contentOffset withTimingFunction:(CAMediaTimingFunction *)timingFunction duration:(CFTimeInterval)duration;

    就像私有 API,但包含所有公共方法和数学。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 1970-01-01
      • 2011-08-24
      • 2011-04-25
      • 1970-01-01
      相关资源
      最近更新 更多