3

我有一个AVPlayerViewControllerin 和自定义的 uicollectionviewcell

如何didSelectItemAtIndexPath在从 AVPLayerViewController 触摸 AVPlayer 时禁用对的调用?

单元格上有一堆其他元素,应该触发didSelectItemAtIndexPath

实际上在播放器控件上它可以工作,但是一旦它们淡出,对 avplayer 的另一次触摸就会触发 didselect。

在同一个单元格中有一个 uibutton 可以正确取消触摸。

4

1 回答 1

1

一个 hacky 解决方案是覆盖您的单元格hitTest并在点击播放器视图时取消选择:

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)
        }
    }
}
于 2019-08-29T11:46:20.090 回答