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

时间:2022-11-22
本文介绍了在 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 上以不同的速度滚动背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:文本量大时UITextView文本被截断 下一篇:iOS UIScrollView 性能

相关文章

最新文章