简体   繁体   中英

How to add cornerRadius to AVPlayerViewController in Swift?

i'm working on video player app that written in Swift .

my problem is i should make AVPlayerViewController corners curve. but i want to use just AVPlayerViewController not any other classes.

what i did now:

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
}

Problem:

what i already have is: 在此输入图像描述

Example Solution:

but i want to have something curve like the one in appstore:

在此输入图像描述

The AVPlayerViewController adds a sub layer to your view. When you set the playerViewController.view.layer.cornerRadius property it only affects it and not its children.

在此输入图像描述

To fix it you must clip the subviews (or here the sublayer) by using the masksToBounds layer property.

playerViewController.view.layer.masksToBounds = true

在此输入图像描述

Try This.

 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() } } 

AVPlayerLayer is OK.

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
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM