& amp; amp; amp; lt; img class=”alignnone wp-image-646 size-full” src=”https://ios.devdon.com/wp-content/uploads/2017/03/Parallax-Gallery.png” alt=”” width=”1024″ height=”618″ srcset=”https://ios.devdon.com/wp-content/uploads/2017/03/Parallax-Gallery.png 1024w, https://ios.devdon.com/wp-content/uploads/2017/03/Parallax-Gallery-300×181.png 300w, https://ios.devdon.com/wp-content/uploads/2017/03/Parallax-Gallery-768×464.png 768w” sizes=”(max-width: 1024px) 100vw, 1024px” /& amp; amp; amp; gt;

Parallax Scrolling has been used frequently in the Web, especially in Landing pages.

In addition to the Layout for pictures, I would like to post some pictures for you. I would like to post pictures for you to see.


Roll parallax effect

Do scroll parallax animation

Parallax effect principle

& amp; amp; amp; lt; img class=”alignnone wp-image-659″ src=”https://ios.devdon.com/wp-content/uploads/2017/03/parallax-life-1.png” alt=”” width=”567″ height=”266″ srcset=”https://ios.devdon.com/wp-content/uploads/2017/03/parallax-life-1.png 862w, https://ios.devdon.com/wp-content/uploads/2017/03/parallax-life-1-300×141.png 300w, https://ios.devdon.com/wp-content/uploads/2017/03/parallax-life-1-768×360.png 768w” sizes=”(max-width: 567px) 100vw, 567px” /& amp; amp; amp; gt; & amp; amp; amp; lt; img class=”alignnone wp-image-660″ src=”https://ios.devdon.com/wp-content/uploads/2017/03/parallax-life-2.png” alt=”” width=”565″ height=”265″ srcset=”https://ios.devdon.com/wp-content/uploads/2017/03/parallax-life-2.png 862w, https://ios.devdon.com/wp-content/uploads/2017/03/parallax-life-2-300×141.png 300w, https://ios.devdon.com/wp-content/uploads/2017/03/parallax-life-2-768×360.png 768w” sizes=”(max-width: 565px) 100vw, 565px” /& amp; amp; amp; gt;

Imagine that our “eyes” now see the “landscape” through a “window,” and when at least one of them moves up and down, the part of the landscape that can be seen changes.

& amp; amp; amp; lt; img class=”alignnone size-full wp-image-658″ src=”https://ios.devdon.com/wp-content/uploads/2017/03/scrolling-parallax-1.png” alt=”” width=”800″ height=”277″ srcset=”https://ios.devdon.com/wp-content/uploads/2017/03/scrolling-parallax-1.png 800w, https://ios.devdon.com/wp-content/uploads/2017/03/scrolling-parallax-1-300×104.png 300w, https://ios.devdon.com/wp-content/uploads/2017/03/scrolling-parallax-1-768×266.png 768w” sizes=”(max-width: 800px) 100vw, 800px” /& amp; amp; amp; gt;

To achieve this effect on a mobile phone screen, we need to prepare an image larger than the Cell

& amp; amp; amp; lt; img class=”alignnone size-large wp-image-657″ src=”https://ios.devdon.com/wp-content/uploads/2017/03/parallax-3-1024×316.png” alt=”” width=”640″ height=”198″ srcset=”https://ios.devdon.com/wp-content/uploads/2017/03/parallax-3-1024×316.png 1024w, https://ios.devdon.com/wp-content/uploads/2017/03/parallax-3-300×92.png 300w, https://ios.devdon.com/wp-content/uploads/2017/03/parallax-3-768×237.png 768w, https://ios.devdon.com/wp-content/uploads/2017/03/parallax-3.png 1402w” sizes=”(max-width: 640px) 100vw, 640px” /& amp; amp; amp; gt;

The screen is our eye, and the screen cannot move, so we can create a parallax effect by moving the Cell or ImageView. The size of the ImageView is 30px (140px — 110px) larger than the Cell, which is the parallax range. After all, we want no content outside the image.

Implementation steps

& amp; amp; amp; lt; img class=”alignnone size-large wp-image-675″ src=”https://ios.devdon.com/wp-content/uploads/2017/03/UICollectionView-Parallax-Scrolling-1024×624.png” alt=”” width=”640″ height=”390″ srcset=”https://ios.devdon.com/wp-content/uploads/2017/03/UICollectionView-Parallax-Scrolling-1024×624.png 1024w, https://ios.devdon.com/wp-content/uploads/2017/03/UICollectionView-Parallax-Scrolling-300×183.png 300w, https://ios.devdon.com/wp-content/uploads/2017/03/UICollectionView-Parallax-Scrolling-768×468.png 768w, https://ios.devdon.com/wp-content/uploads/2017/03/UICollectionView-Parallax-Scrolling.png 1275w” sizes=”(max-width: 640px) 100vw, 640px” /& amp; amp; amp; gt;

Let’s say we’re using a UICollectionView and want the image in the cell to look like a parallax scroll as we slide.

We take the center of the View as the standard,

  • When the Cell is in the middle of the CollectionView, the ImageView center is also in the center of the Cell.
  • When the Cell is at the top of the CollectionView, the ImageView is at the top.
  • When the Cell is under the CollectionView, the ImageView is under the CollectionView.

UIScrollViewDelegate

To achieve this effect, we first need to implement the UIScrollViewDelegate method, which informs all cells on the screen when the screen slides.

// MARK: UIScrollViewDelegate extension HomeViewController: UIScrollViewDelegate { func scrollViewDidScroll(_ scrollView: UIScrollView) {// When UICollectionView is dragged, Notice appeared on the screen of the cell for the cell in aCollectionView. VisibleCells {if let homeCell = cell as? HomeCell { homeCell.cellOnTableView(collectionView: aCollectionView, didScrollOnView: view) } } } }Copy the code

Cell

Provide a method in the Cell to calculate the Cell’s displacement from the center point in the coordinate system of the View(superView of the CollectionView).

Use this displacement to calculate the parallax shift ratio and change the current imageView frame.

Func cellOnTableView (collectionView: UICollectionView, didScrollOnView view: UIView) {/ / cell in collectionView, RectInSuperview = collectionView.convert(self.frame, to: // Let distanceFromCenter = view.frame.height/2 - rectinsuperview. minY Let parallaxHeight = imageView.frame. Height - frame. Height // parallaxHeight = imageView.frame. Let move = (distanceFromCenter/view.frame.height) * parallaxHeight // parallaxHeight /2 Var imageRect = imageview.frame imageRect. Origin. Y = -(parallaxHeight/2) + move // Give the imageView a new frame to achieve the parallax effect. imageView.frame = imageRect }Copy the code

In order for the image to have the correct frame when it is first rendered, we trigger the cell to calculate the frame when the image is first rendered.

Override func viewDidAppear(_ animated: Bool) {// Offset scrollViewDidScroll(aCollectionView)}Copy the code

Recommendation and Reference

  • Welcome to share this article “Swift implementation Parallax Scrolling effect (Parallax Scrolling)” – https://ios.devdon.com/archives/643
  • Learn more about UIView’s coordinate system and its transformation method.
  • SourceCode — “Swift Implements Rolling Parallax Effect” on Gihub