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

Scroll a background in a different speed on a UIScrollView(在 UIScrollView 上以不同的速度滚动背景)
本文介绍了在 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 上以不同的速度滚动背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中设置滚动视图内容大小)
Stop a UITableView from automatically scrolling(阻止 UITableView 自动滚动)
iOS UIScrollView Lazy Loading(iOS UIScrollView 延迟加载)
using iOS 6.0 SDK and building for iOS 5 Target causes UIScrollView setMinimumZoomScale to fail when running on iOS 5 simulator(在 iOS 5 模拟器上运行时,使用 iOS 6.0 SDK 并为 iOS 5 Target 构建会导致 UIScrollView setMinimumZoomScale 失败) -
Create partial-screen UIPageViewController programmatically(以编程方式创建部分屏幕 UIPageViewController)
how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可缩放?)