gpt4 book ai didi

ios - 无法触摸包含 AVPlayerLayer 的 UIView

转载 作者:行者123 更新时间:2023-11-28 05:42:20 26 4
gpt4 key购买 nike

我向 UIImageView 添加了一个层 AVPlayerLayer,并将 UITapGestureRecognizer 添加到 UIImageView。是的,我已经为 UIImageView 启用了委托(delegate)和用户交互。但是水龙头坏了。

有没有人详细了解AVPlayerLayer。

let imageViewTap = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
imageViewTap.numberOfTapsRequired = 1
imageView.addGestureRecognizer(imageViewTap)

let item = AVPlayerItem(asset: videoAsset)
videoPlayer = AVPlayer(playerItem: item)
videoPlayerLayer = AVPlayerLayer(player: videoPlayer)
videoPlayerLayer.frame = imageView.bounds
videoPlayerLayer.backgroundColor = UIColor.black.cgColor
imageView.layer.insertSublayer(videoPlayerLayer, at: 0)

@objc func imageViewTapped(){
print("my code here")
}

最佳答案

检查是否添加了 AVPlayerLayer。在 index O 处添加层,而不是将其添加到顶部。

使用 insertSublayer(_:at:) 而不是 addSublayer(_:)

class ViewController: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet weak var imageView: UIImageView!

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.imageView.isUserInteractionEnabled = true
self.addPlayer()
}

func addPlayer() {
if let url = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4") {
let player = AVPlayer(url: url)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.imageView.bounds
playerLayer.videoGravity = .resizeAspect
self.imageView.layer.insertSublayer(playerLayer, at: 0)
player.play()
}

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapHandler(_:)))
tapGesture.delegate = self
self.imageView.addGestureRecognizer(tapGesture)
}

@objc func tapHandler(_ sender: UIGestureRecognizerDelegate) {
print("view tapped")
}
}

现在,无论何时点击viewtapHandler(_:) 方法都会被调用。

不要忘记手动启用 imageViewuserInteraction

self.imageView.isUserInteractionEnabled = true

关于ios - 无法触摸包含 AVPlayerLayer 的 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56181076/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com