Kodeco Forums

Video Tutorial: Custom Collection View Layouts Part 11: Timbre – Cell Transform

Learn how to rotate the cells and inset the frames slightly so they stay within the bounds of the collection view for a layout like that seen in Timbre.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3987-custom-collection-view-layout/lessons/12

hi, when i done challenge, appear the bug with one and the same cell, it stay big after scrolling to bottom and back to top.
i don’t understood what to do with this bug.

@vladyslavv8 As the error describes it pertains to the fact that in the subclass TimbreLayout the layout attributes aren’t being copied.

This fixed it for me:

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var cache = [UICollectionViewLayoutAttributes]()
if let layoutAttributes = super.layoutAttributesForElementsInRect(rect) as [UICollectionViewLayoutAttributes]?{
    for attributes in layoutAttributes {
      let att = attributes.copy() as! UICollectionViewLayoutAttributes
      let frame = att.frame
      att.transform = CGAffineTransformMakeRotation(degreesToRadians(-14))
      att.frame = CGRectInset(frame, 12, 0)
      cache.append(att)
    }
    
}
return cache

}

2 Likes

A simpler solution is just to adjust the line that calls super, like so:

let layoutAttributes = super.layoutAttributesForElement(in: rect)!.map 
    { $0.copy() as! UICollectionViewLayoutAttributes }

The downcast in the closure is required because copy() is a method on NSObject that returns an Any.

@raywenderlich there’s a weird 1 minute of black screen at the end of part 12 of this course.

@gsharm1 Thank you for the heads up - much appreciated! I will let the video team know. Thanks again!

1 Like