用户登录
用户注册

分享至

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

  • 作者: 少年未老丶心已荒凉
  • 来源: 51数据库
  • 2022-10-21

问题描述

当有人用擦除手势从左到右滚动内容时,我希望背景图像以不同的速度滚动到相同的方向.就像这些经典游戏在 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;
}
软件
前端设计
程序设计
Java相关