Search code examples
iosinterfaceuicollectionviewuislider

UISlider inside UICollectionViews leads does not receive touch events


Currently I'm struggling with receiving touch on a UISlider which is layed out via a xib layouts + a UICollectionView.

My layout which does contain the UISlider does look like this:

the UISlider layout

The UICollectionView itself works perfectly fine. The layout with the UISlider is layed out with several other views.

Currently I experience the UISlider not to react to any user interaction at all. I can scroll inside the collectionView, but when I try to change the value of the slider it just stays as it is.

I found some solutions on SO, like:

Pass UICollectionView touch event to its parent UITableViewCell

How do you stop UITapGestureRecognizer from catching EVERY tap?

but the solutions seem not to be really complete or helpful to me, since I'm quite new to ios and the touch handling.

I'd appreciate any help!


Solution

  • Create a new .swift file. Name it whatever you want and paste in this code:

    import UIKit
    
    class YourClassName: UICollectionViewCell {
    
    
        override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    
            for subview in subviews {
    
                if !subview.isHidden && subview.isUserInteractionEnabled && subview.point(inside: convert(point, to: subview), with: event) {
    
                    return true
                }
    
            }
    
            return false
    
        }
    
    
    }
    

    Then, click on your collection view cell and go to the identity inspector and where it says 'class' input the class name you created earlier. Hope this works!

    EDIT

    Their personal solution is below, but here is a general solution for anyone else who needs to fit this in their code :)