This is a very simple example to achieve the android multi screen wallpaper effect on iOS, written in Swift. Sorry for the poor and redundant English as I am a non-native speaker and new to coding.
Many android phones have a multi screen desktop with the feature that when you swipe between screens, the wallpaper moves horizontally according to your gesture. Usually the wallpaper will move in a smaller scale comparing to the icons or search boxes to convey a sense of depth.
To do this, I use a UIScrollView
instead of a UIPageViewController
.I tried the latter at first, which makes the viewController
hierarchy become very complex, and I couldn't use the touchesMove method correctly, as when you pan around pages, a new child viewController
will come in and interrupt the method.
Instead, I tried UIScrollView
and here is what I have reached.
- drag a wallpaper imageView into
viewController
, set the image, set the frame to (0,0,width of image, height of screen) - set the
viewController
size to freedom and set the width to theNumberOfPagesYouWantToDisplay * screenWidth (only to make the next steps easier, the width of theviewController
will not affect the final result.) - drag a
scrollView
that fits in theviewController
- add the "content UIs" into the
scrollView
to become a childView of it. Or you can do it using codes. - add a
pageControl
if you would like. in your
viewController
.swift add the following codeimport UIKit class ViewController: UIViewController, UIScrollViewDelegate{ let pagesToDisplay = 10 // You can modify this to test the result. let scrollViewWidth = CGFloat(320) let scrollViewHeight = CGFloat(568) // You can also modify these based on the screen size of your device, this is an example for iPhone 5/5s @IBOutlet weak var backgroundView: UIImageView! @IBOutlet weak var scrollView: UIScrollView! @IBOutlet weak var pageControl: UIPageControl! override func viewDidLoad() { super.viewDidLoad() scrollView.delegate = self scrollView.pagingEnabled = true // Makes the scrollView feel like pageViewController. scrollView.frame = CGRectMake(0,0,scrollViewWidth,scrollViewHeight) //Remember to set the frame back to the screen size. scrollView.contentSize = CGSizeMake(CGFloat(pagesToDisplay) * scrollViewWidth, scrollViewHeight) pageControl.numberOfPages = pagesToDisplay } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func scrollViewDidEndDecelerating(scrollView: UIScrollView) { let pageIndex = Int(scrollView.contentOffset.x / scrollView.frame.size.width) pageControl.currentPage = pageIndex } func scrollViewDidScroll(scrollView: UIScrollView) { let bgvMaxOffset = scrollView.frame.width - backgroundView.frame.width let svMaxOffset = scrollView.frame.width * CGFloat(pagesToDisplay - 1) if scrollView.contentOffset.x >= 0 && scrollView.contentOffset.x <= svMaxOffset { backgroundView.frame.origin.x = scrollView.contentOffset.x * (bgvMaxOffset / svMaxOffset) } } }
Feel free to try this on your device, and it will be really appreciated if someone could give some advice on this. Thank you.