Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayerViewController in uicollectionviewcell - cancel touch

I have a AVPlayerViewController in a custom uicollectionviewcell.

How can I disable the call to didSelectItemAtIndexPath when touched on the AVPlayer from AVPLayerViewController?

There are a bunch of other elements on the cell, which should trigger the didSelectItemAtIndexPath.

Actually on the player controls it works, but once they fade out, another touch on the avplayer triggers the didselect.

Having an uibutton in the same cell, cancels the touches correctly.

like image 946
Helmut Januschka Avatar asked Apr 01 '26 18:04

Helmut Januschka


1 Answers

One hacky solution is to override your cell's hitTest and cancel the selection when the player view is tapped :

class MyCell : UITableViewCell {
    @IBOutlet private weak var videoContainer:UIView!

    override func awakeFromNib() {
        super.awakeFromNib()

        // Settings up the video inside the cell :

        let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!
        let player = AVPlayer(url: videoURL)
        playerViewController = AVPlayerViewController()
        playerViewController.player = player
        player.play()

        videoContainer.addSubview(playerViewController.view)
        playerViewController.view.pinEdgesToSuperviewEdges()
    }

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let convertedPoint = videoContainer.convert(point, from: self)
        if let hitView = videoContainer.hitTest(convertedPoint, with: event) {
            // The tap is somewhere inside the video view, 
            // disable user interaction on the cell and continue
            self.isUserInteractionEnabled = false
            return hitView
        }
        else {
            // Tap is outside the video, 
            // use behavior that will trigger `didSelectItemAtIndexPath`
            self.isUserInteractionEnabled = true
            return super.hitTest(point, with: event)
        }
    }
}
like image 53
Axel Guilmin Avatar answered Apr 04 '26 07:04

Axel Guilmin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!