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.
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)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With