Use a line of code in iOS to implement the parallax effect of tableview (collectionview ).
The principle is simple, that is, an equality formula.
Cell Image Position/cell image sliding length=Cell position in table/cell sliding length
Implementation Method:
1. Add the parent view UIView to the cell's UIImageView, and set clipsToBounds to yes to cut the UIImageView and limit its size.
2.
-(Void) scrollViewDidScroll :( UIScrollView *) scrollView
Update the image 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: It is indeed a line of code, haha. In fact, a line of code is just a gimmick, you can write a few more lines to increase readability. The reason for writing a line of code is to express this effect. Calculates the offset and resets the frame.
3. Call
[Self scrollViewDidScroll: nil];
For example
-(Void) viewDidAppear :( BOOL) animated
{
[Super viewDidAppear: YES];
[Self scrollViewDidScroll: nil];
}
Finally, add the source code demo (not easy to post, ask for points, haha)
Simple demo (with comments)
Above