Scrolling background at a different speed on UIScrollView

When someone erases to scroll content from left to right, I would like the background image to scroll in one direction, but at a different speed. Like what these classic games did 20 years ago (remember, somebody ????)

+4
source share
4 answers

I accomplished this using two instances of UIScrollView . First, where the actual content is displayed, and the second (which is behind the first in z-order) is the place where I have a slower background. From there, the top UIScrollView has a delegate attached to it, which gets notified when the contentOffset changes. This delegate, in turn, programmatically sets the contentOffset background scroller multiplied by a constant to slow down the scroll relative to the foreground. So, for example, 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; } 
+5
source

You can easily do this by implementing a scroll view, scrolling with the UIImageView under it ... You will get something like this ... with backgroundImageView being a UIImageView added to the view before the preview ... you can overlay as many images, as much as you want, no performance issues

  - (void)scrollViewDidScroll:(UIScrollView *)scrollView { float factor = scrollView.contentOffset.x / (scrollView.contentSize.width - 320); if (factor < 0) factor = 0; if (factor > 1) factor = 1; CGRect frame = backgroundImageView.frame; frame.origin.x = factor * (320 - backgroundImageView.frame.size.width); backgroundImageView.frame = frame; } 
+3
source

You can do this with CoreAnimation. You will want to connect to the scrollViewDidEndDragging:willDecelerate: and scrollViewWillBeginDecelerating: UIScrollViewDelegate methods. Then start the animation on the image by changing the center position. See this SO article for more information on animation.

+2
source

For example, you have several scrollviews, they need the scroll speed of the difference. here is the base code of the modification on Salamatism:

 CGSize screenSize = [[UIScreen mainScreen] bounds].size; float factor = scrollView.contentOffset.x / (scrollView.contentSize.width - screenSize.width); if (factor < 0) factor = 0; if (factor > 1) factor = 1; CGSize parralaxSize = self.parralaxBackgroundView.contentSize; CGPoint parallaxOffset = CGPointMake(-(factor * (screenSize.width - parralaxSize.width)), 0); [self.parralaxBackgroundView setContentOffset:parallaxOffset animated:NO]; 
0
source

All Articles