无法使用 swift 在 AVPlayerViewController 上捕获滑动手势
unable to catch swipe gesture on AVPlayerViewController using swift
我想检查轻扫手势在 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
}
}
谢谢
您是否在您的视图中启用了用户交互?
playerViewController.view.isUserInteractionEnabled = true
我不了解播放器,但你应该阅读文档,也许它已经捕捉到滑动手势并提供 API 来使用它。
此致,
尝试为 UISwipeGestureRecognizer
设置 delegate
,即
leftSwipe.delegate = self
并使您的 viewController
符合 UIGestureRecognizerDelegate
class ViewController: UIViewController, UIGestureRecognizerDelegate {
//Rest of the code
}
首先你需要 disable AVPlayerViewController
的默认 gestureRecognizers
因为你可以使用
extension AVPlayerViewController {
func disableGestureRecognition() {
let contentView = view.value(forKey: "contentView") as? UIView
contentView?.gestureRecognizers = contentView?.gestureRecognizers?.filter { [=10=] is UITapGestureRecognizer }
}
}
您可以像这样使用它:
playerViewController.disableGestureRecognition()
现在你需要替换
leftSwipe.direction = UISwipeGestureRecognizer.Direction.up
和
leftSwipe.direction = UISwipeGestureRecognizer.Direction.left
因为您正在检查 left
方向。
并且由于您的 UIViewController
不再位于 window 层次结构中,您无法使用 self.present
显示警报并解决您需要将 playerViewController
作为全局class.
中的变量
然后您可以像这样显示警报:
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 { [=15=] is UITapGestureRecognizer }
}
}
结果将是:
我想检查轻扫手势在 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
}
}
谢谢
您是否在您的视图中启用了用户交互?
playerViewController.view.isUserInteractionEnabled = true
我不了解播放器,但你应该阅读文档,也许它已经捕捉到滑动手势并提供 API 来使用它。
此致,
尝试为 UISwipeGestureRecognizer
设置 delegate
,即
leftSwipe.delegate = self
并使您的 viewController
符合 UIGestureRecognizerDelegate
class ViewController: UIViewController, UIGestureRecognizerDelegate {
//Rest of the code
}
首先你需要 disable AVPlayerViewController
的默认 gestureRecognizers
因为你可以使用
extension AVPlayerViewController {
func disableGestureRecognition() {
let contentView = view.value(forKey: "contentView") as? UIView
contentView?.gestureRecognizers = contentView?.gestureRecognizers?.filter { [=10=] is UITapGestureRecognizer }
}
}
您可以像这样使用它:
playerViewController.disableGestureRecognition()
现在你需要替换
leftSwipe.direction = UISwipeGestureRecognizer.Direction.up
和
leftSwipe.direction = UISwipeGestureRecognizer.Direction.left
因为您正在检查 left
方向。
并且由于您的 UIViewController
不再位于 window 层次结构中,您无法使用 self.present
显示警报并解决您需要将 playerViewController
作为全局class.
然后您可以像这样显示警报:
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 { [=15=] is UITapGestureRecognizer }
}
}
结果将是: