在 UIScrollView 上以不同的速度滚动背景

编程入门 行业动态 更新时间:2024-10-24 16:23:01
本文介绍了在 UIScrollView 上以不同的速度滚动背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当有人用擦除手势从左到右滚动内容时,我希望背景图像以不同的速度滚动到相同的方向.就像这些经典游戏在 20 年前所做的一样(记住这一点,有人吗????)

When somebody does a wipe gesture to scroll the content from left to right, I would like to have a background image scrolling into the same direction, but at a different speed. Much like what these classic games did do 20 years ago (remember that, anybody????)

推荐答案

我通过使用 两个 UIScrollView 实例完成了这个.第一个是显示实际内容的位置,第二个(在 z 顺序中位于第一个之后)是我移动较慢的背景的位置.从那里开始,顶部的 UIScrollView 附加了一个委托,当 contentOffset 更改时会收到通知.反过来,该委托以编程方式设置背景滚动条的 contentOffset,乘以一个常数以减慢相对于前景的滚动速度.因此,例如,您可能有以下内容:

I accomplished this by using two UIScrollView instances. The first is where the actual content is displayed, and the second (which is behind the first in z-order) is where I have my slower-moving background. From there the top UIScrollView has a delegate attached to it that gets notified when the contentOffset changes. That delegate, in turn, programatically sets the contentOffset of the background scroller, multiplied against a constant to slow the scroll down relative to the foreground. So, for instance, you might have something like:

// Defined as part of the delegate for the foreground UIScrollView - (void)scrollViewDidScroll:(UIScrollView *)scrollView { UIScrollView* scroll_view(static_cast<UIScrollView*>(bkg_scroller_m.view)); CGPoint offset(scrollView.contentOffset); offset.x = offset.x / 3; offset.y = offset.y / 3; // Scroll the background scroll view by some smaller offset scroll_view.contentOffset = offset; }

更多推荐

在 UIScrollView 上以不同的速度滚动背景

本文发布于:2023-07-18 04:35:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141072.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上以   背景   速度   UIScrollView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!