Parallax Effect of TableView (CollectionView) with one line of code under iOS

Source: Internet
Author: User

The effect is the same as Https://github.com/jberlana/JBParallaxCell. Implements the cell's rolling parallax.


The principle is very simple, is a equal ratio formula

Cell picture position/cell picture can slide the total length = cell in table position/cell can slide total length


Implementation method:

1. Add the parent view UIView on the cell's Uiimageview , Clipstobounds is set to Yes, which is used to cut the Uiimageview and limit its size.


2 " in the VC

-(void) Scrollviewdidscroll: ( Uiscrollview *) ScrollView

To update the picture location of each cell in real time.

The code is as follows


-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{for    (Weltableviewcell * cell in [Self.tableview Visiblecells])    {        cell.image.frame = cgrectoffset (cell.image.frame, 0, ((Cell.image.frame.size.height- Cell.imageFrameView.frame.size.height) * ((CELL.FRAME.ORIGIN.Y + cell.imageFrameView.frame.size.height- SELF.TABLEVIEW.CONTENTOFFSET.Y)/(Self.tableView.frame.size.height + cell.imageFrameView.frame.size.height * 2)) + Cell.imageframeview.frame.size.height-cell.image.frame.size.height)-CELL.IMAGE.FRAME.ORIGIN.Y);}    }

PS: Really is a line of code, haha. In fact, a line of code is just a gimmick, you can write more than a few lines to increase readability. The reason to write a line of code is to express this effect is actually quite simple. The offset is calculated by using a calculation, and the frame is reset.


3 "After the initial refresh of TableView, call

[self scrollviewdidscroll:nil];

For example

-(void) Viewdidappear: (BOOL) animated

{

[superviewdidappear:YES];

[self scrollviewdidscroll:nil];

}


Finally, attach the source code demo (post not easy, to get an integral, haha)

Simple demo (with notes)


Above


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.