1

I came across this demo and really want to learn how to do this:

enter image description here

So far, I've created a subclass to UICollectionViewFlowLayout that fades out the start and end of the cells. Taken from https://gist.github.com/vinhnx/bb1354b247ebfe3790563173ac72baa9

This is the part that fades out the start and end cells

for attrs in attributes {
    if attrs.frame.intersects(rect) {
        let distance = visibleRect.midY - attrs.center.y
        let normalizedDistance = abs(distance) / (visibleRect.height * fadeFactor)
        let fade = 1 - normalizedDistance
        attrs.alpha = fade
    }
}

How do I make the cells that is at the top stand still and not continue to slide up?

Full code of the function handling the fade:

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    let attributesSuper: [UICollectionViewLayoutAttributes] = super.layoutAttributesForElements(in: rect) as [UICollectionViewLayoutAttributes]!
    if let attributes = NSArray(array: attributesSuper, copyItems: true) as? [UICollectionViewLayoutAttributes]{
        var visibleRect = CGRect()
        visibleRect.origin = collectionView!.contentOffset
        visibleRect.size = collectionView!.bounds.size
        for attrs in attributes {
            if attrs.frame.intersects(rect) {
                let distance = visibleRect.midY - attrs.center.y
                let normalizedDistance = abs(distance) / (visibleRect.height * fadeFactor)
                let fade = 1 - normalizedDistance
                attrs.alpha = fade
                print(fade)
            }
        }
        return attributes
    }else{
        return nil
    }
}

The look of my app right now: enter image description here

Jonas Borneland
  • 383
  • 1
  • 6
  • 19
  • @JoshCaswell I've updated the question :). How do I make the cells that is at the top stand still and not continue to slide up? – Jonas Borneland Mar 18 '18 at 19:34
  • the cell will keep moving up. you can play with it but its not recommended. you need to be having an inside parent view that changes its frame. – hasan Mar 18 '18 at 19:40
  • and you also needs to set clipToBounds to false and send cells top cells to back. sendSubviewToBack – hasan Mar 18 '18 at 19:43
  • @hasan83 Thanks!! But I don't know how to reach the subview? I can get attrs.frame.origin.y ...but attrs.subview[0].frame.origin.y dosen´t work – Jonas Borneland Mar 19 '18 at 09:49

0 Answers0