gpt4 book ai didi

ios - 将 AVPlayer subview 添加到 UIView

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

我正在使用以下代码创建一个启用了分页的垂直 ScrollView ,它工作得很好。现在我想将类型为 AVPlayer (?) 的 subview 添加到 homeViewview2view3。我可以将 setupCustomPlayer() 放在 initiateScrollView() 函数中,效果也很好。但是,它不附属于任何前面提到的观点。如果我尝试使用函数 addSubview(anything) 添加 subview ,它会告诉我需要一个 UIView。我需要 UIViews,因为垂直滑动提要非常重要。有人知道如何将它添加为 subview 吗?该视频将覆盖整个屏幕,并作为背景视频(如果您想知道的话)

代码:

func setupCustomPlayer() {

print("check")

AVPlayerVC.view.frame = self.view.frame
AVPlayerVC.view.sizeToFit()
AVPlayerVC.showsPlaybackControls = false
self.view.addSubview(AVPlayerVC.view)

let videoURL = NSURL(string: "http://URL")


let player = AVPlayer(url: videoURL! as URL)
AVPlayerVC.player = player
AVPlayerVC.player?.play()
}

func initiateScrollView() {

//create scrollView with paging enabled
let scrollView = UIScrollView(frame: view.bounds)
scrollView.isPagingEnabled = true
view.addSubview(scrollView)

//do not show vertical scroll indicator
scrollView.showsVerticalScrollIndicator = false;

//get page size
let pageSize = view.bounds.size

//individual views
let homeView = UIView()
homeView.backgroundColor = .green

let view2 = UIView()
view2.backgroundColor = .blue

let view3 = UIView()
view3.backgroundColor = .red

//array with individual views
let pagesViews = [homeView, view2, view3]

//amount of views
let numberOfPages = pagesViews.count

//add subviews (pages)
for (pageIndex, page) in pagesViews.enumerated(){
page.frame = CGRect(origin: CGPoint(x:0 , y:CGFloat(pageIndex) * pageSize.height), size: pageSize)
scrollView.addSubview(page)
}

//define size of scrollView
scrollView.contentSize = CGSize(width: pageSize.width, height: pageSize.height * CGFloat(numberOfPages))

}

最佳答案

你能试试这个吗?

func setupCustomPlayer() {

print("check")
let player = AVPlayer(url: yourFileUrl)
var playerLayer: AVPlayerLayer?
playerLayer = AVPlayerLayer(player: player)
playerLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
playerLayer!.frame = self.view.frame
self.view.layer.addSublayer(playerLayer!)

player.play()
}

关于ios - 将 AVPlayer subview 添加到 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43968452/

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