Swift-如何根据视频播放器框架的大小动态调整UIView的大小

时间:2019-05-31 06:12:14

标签: ios swift uiview avfoundation avplayer

我有一个视频播放器和一个UIView覆盖它,用于视频区域中的Gesture Recognition。我这样做是为了您可以点按视频的不同区域来播放,倒带和执行其他功能。

目前,此功能在横向上可以正常使用,但是如果我将设备旋转为纵向模式。 UIView不会调整大小,但是视频播放器会调整大小。我尝试以编程方式使用约束,但无法弄清楚如何访问视频播放器的NSLayout锚点。

我有一个函数可以检索视频帧的大小,当前用于将UIView设置为视频播放器的大小。

下面的代码可以直接复制到项目中以播放视频并显示我要调整的UIView。您只需要将UIView添加到UIViewController故事板中。该代码不包含GestureRecogniser部分。

import UIKit
import AVFoundation

class ViewController: UIViewController {

let controlContainerView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor(white: 0, alpha: 1)
    return view
}()

let activityIndicatorView: UIActivityIndicatorView = {
    let aiv = UIActivityIndicatorView(style: .whiteLarge)
    aiv.translatesAutoresizingMaskIntoConstraints = false
    aiv.startAnimating()
    return aiv
}()

@IBOutlet weak var videoContainer: UIView!
var player: AVPlayer?
var playerLayer: AVPlayerLayer?
var tapRegionBounds: UIView!
var tapRegionAreaCreated: Bool = false

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    let urlString = "https://www.radiantmediaplayer.com/media/bbb-360p.mp4"
    videoPlayer(filename: urlString)
    player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)

}

override func viewDidAppear(_ animated: Bool) {
    self.playerLayer?.frame = CGRect(x: 0, y: 0, width: self.videoContainer.frame.width, height: self.videoContainer.frame.height)
    updateTapRegions()
}

func videoPlayer(filename: String){
    let item = AVPlayerItem(url: URL(string: filename)!)
    player = AVPlayer(playerItem: item)
    playerLayer = AVPlayerLayer(player: player)
    playerLayer?.videoGravity = .resizeAspect
    playerLayer?.frame = CGRect(x: 0, y: 0, width: self.videoContainer.frame.width, height: self.videoContainer.frame.height)
    videoContainer.layer.addSublayer(playerLayer!)
    player?.play()
}


override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "currentItem.loadedTimeRanges" {
        activityIndicatorView.stopAnimating()
        controlContainerView.backgroundColor = .clear
    }
}

func videoFrame() -> CGRect? {
    guard let layer = playerLayer
        else { return nil }
    let frame = layer.videoRect
    if frame.height > 0.0 {
        let frameInWindow = layer.convert(frame, to: nil)
        return view.convert(frameInWindow, from: nil)
    } else {
        return nil
    }
}

//THE CODE BELOW KIND OF WORKS TO SHOW HOW I WANT IT TO WORK BUT WITHOUT HAVING TO DELETE THE VIEW EACH TIME
func updateTapRegions() {
    guard let video = videoFrame() else { return }
    let container = videoContainer.bounds

    if tapRegionAreaCreated == true {
        tapRegionBounds.removeFromSuperview()
        tapRegionAreaCreated = false
    }

    tapRegionBounds = UIView()
    tapRegionBounds.frame = CGRect(x: video.minX, y: video.minY, width: container.width, height: video.height)
    tapRegionBounds?.alpha = 0.5
    tapRegionBounds.backgroundColor = UIColor.red

    if tapRegionAreaCreated == false {
        view.addSubview(tapRegionBounds)
        tapRegionAreaCreated = true
    }
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    coordinator.animate(alongsideTransition: { (context) in
    }) { (context) in
        self.playerLayer?.frame = CGRect(x: 0, y: 0, width: self.videoContainer.frame.width, height: self.videoContainer.frame.height)
        self.updateTapRegions()
    }
}

}

我尝试更新viewWillLayoutSubviews()中的框架,但也没有任何改变。我知道我的updateTapRegions函数不是正确的方法,但是如果有人能指出正确的方向,那将非常感谢。

2 个答案:

答案 0 :(得分:2)

不幸的是,您没有在问题中说出如何调整这一切所依赖的视频容器视图的大小。但这是有可能的。我使用自动布局以纵向和横向获得这些结果:

enter image description here

enter image description here

现在假设我们有一个子层(一个AVPlayerLayer)和一个子视图(点击手势视图)。好了,可以使用自动布局调整子视图的大小以自动适应!因此只剩下播放器层;它不会使用自动布局自动调整大小,但是您可以在viewDidLayoutSubviews中手动调整其大小。

答案 1 :(得分:0)

最后,我通过将代码从过渡功能(即使视频播放器正确调整了大小)移动到viewDidLayoutSubviews上来进行了工作,然后正常工作。而且,我不再每次设备更改方向时都创建和删除视图。

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    self.playerLayer?.frame = CGRect(x: 0, y: 0, width: self.videoContainer.frame.width, height: self.videoContainer.frame.height)

    guard let video = videoFrame() else { return }

    if tapRegionBounds != nil {
         self.tapRegionBounds.frame = CGRect(x: video.minX, y: video.minY, width: video.width, height: video.height)
    }
}