UICollectionViewCell resizes cells when scrolling

I am trying to create a UICollectionView where the UICollectionViewCell becomes reduced when it "leaves" the visible area above or below. And increasing the size to a normal size, "entering" the visible area.

I tried the zoom / animation code in: scrollViewDidScroll() , but I can't figure it out correctly.

My full function looks like this:

  func scrollViewDidScroll(scrollView: UIScrollView) { var arr = colView.indexPathsForVisibleItems() for indexPath in arr{ var cell = colView.cellForItemAtIndexPath(indexPath as! NSIndexPath)! var pos = colView.convertRect(cell.frame, toView: self.view) if pos.origin.y < 50 && pos.origin.y >= 0{ cell.hidden = false UIView.animateWithDuration(0.5, animations: { () -> Void in cell.transform = CGAffineTransformMakeScale(0.02 * pos.origin.y, 0.02 * pos.origin.y) }) }else if pos.origin.y == 50{ UIView.animateWithDuration(0.5, animations: { () -> Void in cell.transform = CGAffineTransformMakeScale(1, 1) }) } } } 

Is this somehow the right approach, or is there another better way?

+5
source share
1 answer

Not a complete solution, but a few notes / pointers:

  • You should not directly contact the collection view cells in this way, but have your own UICollectionViewLayout subclass that modifies the UICollectionViewLayoutAttributes to include the desired transform and, if necessary, invalidate the layout.

  • Doing if pos.origin.y == 50 is certainly not a good idea, because scrolling may not go through all the values ​​(that is, it can jump from 45 to 53). Thus, use >= and enable another way if you want your animation to run only once at the "border" (for example, save the last position or flag).

+4
source

All Articles