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