如何在 Swift 中将 cornerRadius 添加到 AVPlayerViewController?

How to add cornerRadius to AVPlayerViewController in Swift?

我正在开发用 Swift 编写的视频播放器应用程序。

我的问题是我应该 AVPlayerViewController 拐角曲线。但我只想使用 AVPlayerViewController 而不是任何其他 类.

我现在做了什么:

fileprivate func setupPlayer() {
    let player = AVPlayer(url: videoURL)
    let playerViewController = AVPlayerViewController()
    playerViewController.view.frame = CGRect.init(x: xPosition,
                                                  y: yPosition,
                                                  width: 200,
                                                  height: 100)
    playerViewController.player = player
    self.addChild(playerViewController)
    self.view.addSubview(playerViewController.view)
    playerViewController.didMove(toParent: self)
    playerViewController.videoGravity = AVLayerVideoGravity.init(rawValue: "")
    playerViewController.view.backgroundColor = UIColor(displayP3Red: 0/255, green: 0/255, blue: 0/255, alpha: 0)
    playerViewController.view.layer.cornerRadius = 20
    playerViewController.contentOverlayView?.isHidden = true
    playerViewController.contentOverlayView?.alpha = 0
}

问题:

我已经拥有的是:

示例解决方案:

但我想要像 appstore 中那样的曲线:

试试这个。

import UIKit
import AVKit

class ViewController: UIViewController {
    var videoController:AVPlayerViewController!
    var player:AVPlayer!
    var url:URL!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let path:String=Bundle.main.path(forResource: "PinkLake", ofType: "mp4")!
        url=URL(fileURLWithPath: path)
        
        player=AVPlayer(url: url)
        videoController=AVPlayerViewController()
        videoController.player=player;
       
        let viewVideo=UIView()
        viewVideo.frame.origin.x=0;
        viewVideo.frame.origin.y=50;
        viewVideo.frame.size.width=200;
        viewVideo.frame.size.height=200;
        videoController.view.frame=viewVideo.frame
        
        //declare tempView
        var tempView = UIView(frame: videoController.view.frame)
        tempView=videoController.view;
        tempView.layer.cornerRadius=50;
        videoController.view=tempView;
        
        
        self.view.addSubview(viewVideo)
        viewVideo.addSubview(videoController.view)
        self.view.backgroundColor=UIColor.yellow
        player.play()
    }


}

AVPlayerViewController 向您的视图添加了一个子图层。当您设置 playerViewController.view.layer.cornerRadius 属性 时,它只会影响它而不影响它的子项。

要修复它,您必须使用 masksToBounds 图层 属性.

剪辑子视图(或此处的子图层)
playerViewController.view.layer.masksToBounds = true

AVPlayerLayer 正常

import UIKit
import AVKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        if let url = URL(string: "SOMEVIDEOURL.mp4") {
            let player = AVPlayer(url: url)
            let layer = AVPlayerLayer(player: player)
            layer.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
            view.layer.addSublayer(layer)
            layer.cornerRadius = 20
        }
    }
}