     }
     
     override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
-        let layoutAttributesObjects = super.layoutAttributesForElements(in: rect)
+        // We may not change the original layout attributes or UICollectionViewFlowLayout might complain.
+        let layoutAttributesObjects = copy(super.layoutAttributesForElements(in: rect))
         layoutAttributesObjects?.forEach({ (layoutAttributes) in
             setFrame(forLayoutAttributes: layoutAttributes)
         })

        //    is not always left-aligned and the last item in a line is not always right-aligned:
        //    If there is only one item in a line UICollectionViewFlowLayout will center it.
        
        guard let layoutAttributes = super.layoutAttributesForItem(at: indexPath),
            let alignmentAxis = alignmentAxis else {
            return nil
        }
        
        if layoutAttributes.representsClosestItemToAlignmentAxis(collectionViewLayout: self) {

        return lineFrame.intersects(secondItemAttributes.frame)
    }
    
    /// Creates a deep copy of the passed array by copying all its items.
    ///
    /// - Parameter layoutAttributesArray: The array to be copied.
    /// - Returns: A deep copy of the passed array.
    private func copy(_ layoutAttributesArray: [UICollectionViewLayoutAttributes]?) -> [UICollectionViewLayoutAttributes]? {
        return layoutAttributesArray?.map{ $0.copy() } as? [UICollectionViewLayoutAttributes]
    }
    
}


