使用UIScrollView子视图的视差效果 [英] Parallax effect with UIScrollView subviews

查看:105
本文介绍了使用UIScrollView子视图的视差效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在UIScrollView内的UIView上创建一个Parallax效果。
效果似乎有效,但效果不是很好。

I'm trying to create a Parallax effect on a UIView inside a UIScrollView. The effect seems to work, but not so well.


  1. 首先,我将两个UIView子视图添加到UIScrollView并设置UIScrollViews contentSize。

  2. 视图总结并创建了一个{320,1000}的contentSize。

  3. 然后我在scrollViewDidScroll中实现了以下内容:

  1. First i add two UIView sub-views to a UIScrollView and set the UIScrollViews contentSize.
  2. The Views sum up and create a contentSize of {320, 1000}.
  3. Then I implemented the following in scrollViewDidScroll:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;

    CGFloat percentage = offsetY / scrollView.contentSize.height;

    NSLog(@"percent = %f", percentage);

    if (offsetY < 0) {

        firstView.center = CGPointMake(firstView.center.x, firstView.center.y - percentage * 10);

    } else if (offsetY > 0){

        firstView.center = CGPointMake(firstView.center.x, firstView.center.y + percentage * 10);

    }
}


这些代码行确实会产生视差效果,但是当滚动继续时,如果我滚动到原始起始位置,视图不会返回到原始位置。

These lines of code do create a parallax effect, but as the scrolling continues, the view does not return to it's original position if i scroll to the original starting position.

我尝试过操作视图图层和框架,所有这些都有相同的结果。

I have tried manipulating the views layers and frame, all with the same results.

任何帮助都将非常感激。

Any Help will be much appreciated.

推荐答案

您遇到的问题是您的二次滚动基于偏移量与大小的比率,而不仅仅是当前的抵消。因此,当您从99的偏移量增加到100(超过100)时,辅助滚动增加10,但当您返回到99时,辅助滚动仅减少9.9,因此不再位于同一位置这是你最后一次在99.非线性滚动是可能的,但不是你的方式。

The problem you have is that you are basing your secondary scrolling on a ratio of offset to size, not just on the current offset. So when you increase from an offset of 99 to 100 (out of say 100) your secondary scroll increases by 10, but when you go back down to 99 your secondary scroll only decreases by 9.9, and is thereby no longer in the same spot as it was last time you were at 99. Non-linear scrolling is possible, but not the way you are doing it.

一个可能更简单的方法来解决这个问题是创建第二个scrollview并将其放在实际的scrollview下面。使它非棘手( setUserInteractionEnabled:false )并在主滚动委托期间修改它的contentOffset,而不是尝试手动移动UIImageView。

A possible easier way to deal with this is to create a second scrollview and place it below your actual scrollview. Make it non intractable (setUserInteractionEnabled:false) and modify it's contentOffset during the main scrolling delegate instead of trying to move a UIImageView manually.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [scrollView2 setContentOffset:CGPointMake(scrollView.contentOffset.x,scrollView.contentOffset.y * someScalingFactor) animated:NO];
}

但请确保不要为设置委托scrollView2 ,否则你可能会得到一个循环的委托方法调用,这对你来说不会很好。

But make sure not to set a delegate for the scrollView2, otherwise you may get a circular delegate method call that will not end well for you.

这篇关于使用UIScrollView子视图的视差效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆