Search code examples
iosswiftanimationuicollectionviewcellcollectionview

Custom animation for a collection view


I'm learning how to use animations for collectionView on the example of very nice CollectionViewPagingLayout template. This one: https://github.com/amirdew/CollectionViewPagingLayout

Everything works well for the first object in contentView.

enter image description here

But it does not recognise other subviews. If among subviews is a UILabel - all names are displayed at once (though it can be seen that they are changing). Here is a 5-sec video how it looks like

I guess I should either merge all subviews to one view - or modify the template so it recognizes all subviews. But without help I dont understand how to implement either of that.

The author of the template has provided a detailed documentation how to use it: https://github.com/amirdew/CollectionViewPagingLayout/blob/master/Lib/TransformableView.swift

But though it is obviously well written - I failed to properly read it. I feel this lines may solve the problem, but adding them to the code didnt change anything:

public extension TransformableView where Self: UICollectionViewCell {
    
    /// Default `selectableView` for `UICollectionViewCell` is the first subview of
    /// `contentView` or the content view itself if there is no subview
    var selectableView: UIView? {
        contentView.subviews.first ?? contentView
    }
}

And this one

public extension UICollectionViewCell {
    /// This method transfers the event to `selectableView`
    /// this is necessary since cells are on top of each other and they fill the whole collectionView frame
    /// Without this, only the first visible cell is selectable
    // swiftlint:disable:next override_in_extension
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if let view = (self as? TransformableView)?.selectableView {
            return view.hitTest(convert(point, to: view), with: event)
        }
        return super.hitTest(point, with: event)
    }
}

Your help with this issue will be very much appreciated.


Solution

  • The code and documentation you linked to states:

    /// Default `selectableView` for `UICollectionViewCell` is the first subview of
    /// `contentView` or the content view itself if there is no subview
    

    So, make the "first subview" of you cell a "container" UIView, and embed your multiple subviews in that view:

    enter image description here