0
  • Hello, I have followed raywenderlich video for collection view custom layout. UICollectionViewFlowLayout is subclassed as below

    import UIKit

    class CharacterFlowLayout: UICollectionViewFlowLayout {

    let standardItemAlpha: CGFloat = 0.5
    let standardItemScale: CGFloat = 0.5

    var count = 0 override func prepare() {

    super.prepare()

    }

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

   let attributes = super.layoutAttributesForElements(in: rect)

   var attributesCopy = [UICollectionViewLayoutAttributes]()

   for itemAttributes in attributes! {
     let itemAttributesCopy = itemAttributes.copy() as! UICollectionViewLayoutAttributes

     changeLayoutAttributes(itemAttributesCopy)

     attributesCopy.append(itemAttributesCopy)

   }

   return attributesCopy  

}

    **override open func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool** {

   return true

   }

    **func changeLayoutAttributes(_ attributes: UICollectionViewLayoutAttributes)** {


   let collectionCenter = collectionView!.frame.size.height/2

   let offset = collectionView!.contentOffset.y // bounds

   let normalizedCenter = attributes.center.y - offset

   let maxDistance = self.itemSize.height + self.minimumLineSpacing

   let distance = min(abs(collectionCenter - normalizedCenter), maxDistance)

   let ratio = (maxDistance - distance)/maxDistance

   let alpha = ratio * (1 - self.standardItemAlpha) + self.standardItemAlpha

   let scale = ratio * (1 - self.standardItemScale) + self.standardItemScale

   attributes.alpha = alpha

   attributes.transform3D =  CATransform3DScale(CATransform3DIdentity, scale, scale, 1)
      }    }

I don't understand the logic behind the calculation? Could you please explain why we are calculating distance and ratio?

  • Welcome to Stack Overflow. Please take time to [edit] and format your question correctly. – Ashley Mills Sep 22 '18 at 10:36
  • Please check the formatting of your code again. Remove unnecessary comments. If it's hard to read, it's hard to answer. Please take the time and show consideration for those reading your question. – Ashley Mills Sep 22 '18 at 10:42

0 Answers0