Scroll a background in a different speed on a UIScrollView

前端 未结 4 1022
青春惊慌失措
青春惊慌失措 2021-01-13 13:32

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 spe

4条回答
  •  不知归路
    2021-01-13 13:54

    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(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;
    }
    

提交回复
热议问题