gpt4 book ai didi

ios - 无法使用 swift 在 AVPlayerViewController 上捕获滑动手势

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:58:56 25 4
gpt4 key购买 nike

我想检查轻扫手势在 AVPlayer 视频上是否有效。我尝试了一个小示例应用程序来这样做,但它没有用。不确定为什么代码不起作用。

点击按钮,下面是代码:

var playerLayer: AVPlayerLayer?
var player: AVPlayer?

let videoUrlString = "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
let videoURL = URL(string: videoUrlString)
self.player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
present(playerViewController, animated: true, completion: {
self.player?.play()
})

let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
leftSwipe.direction = UISwipeGestureRecognizer.Direction.up

playerViewController.view.addGestureRecognizer(leftSwipe)

@objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {

if (sender.direction == .left) {

let alert = UIAlertController(title: "Swipe Action", message: "It is LEFT Swipe ", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true
}
}

谢谢

最佳答案

首先你需要disable您可以使用 AVPlayerViewController 的默认 gestureRecognizers

extension AVPlayerViewController {
func disableGestureRecognition() {
let contentView = view.value(forKey: "contentView") as? UIView
contentView?.gestureRecognizers = contentView?.gestureRecognizers?.filter { $0 is UITapGestureRecognizer }
}
}

你可以像这样使用它:

playerViewController.disableGestureRecognition()

现在你需要更换

leftSwipe.direction = UISwipeGestureRecognizer.Direction.up

leftSwipe.direction = UISwipeGestureRecognizer.Direction.left

因为您正在检查方向。

并且由于您的 UIViewController 不再位于窗口层次结构中,您无法使用 self.present 显示警报并解决您需要制作 playerViewController 作为类中的全局变量。

然后您可以像这样显示警报:

playerViewController.present(alert, animated: true)

在您的 handleSwipes 方法中。

工作代码如下:

import UIKit
import AVKit

class ViewController: UIViewController {

var playerLayer: AVPlayerLayer?
var player: AVPlayer?
let playerViewController = AVPlayerViewController()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@IBAction func tap(_ sender: Any) {

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

playerViewController.player = player

playerViewController.disableGestureRecognition()
present(playerViewController, animated: true, completion: {
self.player?.play()
})

let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
leftSwipe.direction = UISwipeGestureRecognizer.Direction.left
playerViewController.view.addGestureRecognizer(leftSwipe)
}

@objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {

if (sender.direction == .left) {

let alert = UIAlertController(title: "Swipe Action", message: "It is LEFT Swipe ", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
playerViewController.present(alert, animated: true)
}
}
}

extension AVPlayerViewController {
func disableGestureRecognition() {
let contentView = view.value(forKey: "contentView") as? UIView
contentView?.gestureRecognizers = contentView?.gestureRecognizers?.filter { $0 is UITapGestureRecognizer }
}
}

结果将是:

enter image description here

关于ios - 无法使用 swift 在 AVPlayerViewController 上捕获滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56145912/

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