简体   繁体   中英

AVPlayerViewController in uicollectionviewcell - cancel touch

i am having a AVPlayerViewController in and 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, wich 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.

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)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM