Ios 在UIScrollView中调整AVPlayer的大小
我有一个Ios 在UIScrollView中调整AVPlayer的大小,ios,swift,uiscrollview,Ios,Swift,Uiscrollview,我有一个avplayervcontroller,它的AVPlayer视图作为UIView的子视图添加。此UIView添加到UIScrollView中。其想法是实现视频的“移动和缩放”视图。当UIScrollView中的视图是UIImageView时,我可以使用它。但是,当视图是带有AVPlayer子视图的UIView时,UIView的比例显得太小 这是我的密码: override func viewDidLoad() { super.viewDidLoad() videoPla
avplayervcontroller
,它的AVPlayer
视图作为UIView
的子视图添加。此UIView
添加到UIScrollView
中。其想法是实现视频的“移动和缩放”视图。当UIScrollView
中的视图是UIImageView
时,我可以使用它。但是,当视图是带有AVPlayer
子视图的UIView
时,UIView
的比例显得太小
这是我的密码:
override func viewDidLoad() {
super.viewDidLoad()
videoPlayerController = VideoPlayerController()
videoPlayerController.videoURL = url
self.addChildViewController(videoPlayerController)
videoView.addSubview(videoPlayerController.view)
videoPlayerController.view.translatesAutoresizingMaskIntoConstraints = false
let left = NSLayoutConstraint(item: videoPlayerController.view, attribute: .left, relatedBy: .equal, toItem: videoView, attribute: .left, multiplier: 1, constant: 0)
let right = NSLayoutConstraint(item: videoPlayerController.view, attribute: .right, relatedBy: .equal, toItem: videoView, attribute: .right, multiplier: 1, constant: 0)
let top = NSLayoutConstraint(item: videoPlayerController.view, attribute: .top, relatedBy: .equal, toItem: videoView, attribute: .top, multiplier: 1, constant: 0)
let bottom = NSLayoutConstraint(item: videoPlayerController.view, attribute: .bottom, relatedBy: .equal, toItem: videoView, attribute: .bottom, multiplier: 1, constant: 0)
videoView.addConstraints([left, right, top, bottom])
let asset = AVAsset(url: url)
let clipVideoTrack = asset.tracks(withMediaType: AVMediaTypeVideo).first!
videoView.frame = CGRect(x: 0, y: 0, width: clipVideoTrack.naturalSize.height, height: clipVideoTrack.naturalSize.width)
setZoomScale()
}
fileprivate func setZoomScale() {
let widthScale = scrollView.bounds.size.width / videoView.bounds.size.width
scrollView.minimumZoomScale = widthScale
scrollView.zoomScale = widthScale
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return videoView
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
let videoViewSize = videoView.frame.size
let scrollViewSize = scrollView.bounds.size
let verticalPadding = videoViewSize.height < scrollViewSize.height ? (scrollViewSize.height - videoViewSize.height) / 2 : 0
let horizontalPadding = videoViewSize.width < scrollViewSize.width ? (scrollViewSize.width - videoViewSize.width) / 2 : 0
}
scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}
override func viewDidLoad(){
super.viewDidLoad()
videoPlayerController=videoPlayerController()
videoPlayerController.videoURL=url
self.addChildViewController(视频播放控制器)
videoView.addSubview(videoPlayerController.view)
videoPlayerController.view.Translates自动调整大小GMaskintoConstraints=false
let left=NSLayoutConstraint(项:videoPlayerController.view,属性:。left,relatedBy:。相等,toItem:videoView,属性:。left,乘数:1,常数:0)
let right=NSLayoutConstraint(项:videoPlayerController.view,属性:。右侧,关联方:。相等,toItem:videoView,属性:。右侧,乘数:1,常数:0)
让top=NSLayoutConstraint(项:videoPlayerController.view,属性:.top,relatedBy:.equal,toItem:videoView,属性:.top,乘数:1,常数:0)
let bottom=NSLayoutConstraint(项:videoPlayerController.view,属性:.bottom,relatedBy:.equal,toItem:videoView,属性:.bottom,乘数:1,常数:0)
videoView.addConstraints([左、右、上、下])
let asset=AVAsset(url:url)
让clipVideoTrack=asset.tracks(使用MediaType:AVMediaTypeVideo)。首先!
videoView.frame=CGRect(x:0,y:0,宽度:clipVideoTrack.naturalSize.height,高度:clipVideoTrack.naturalSize.width)
setZoomScale()
}
fileprivate func setZoomScale(){
让widthScale=scrollView.bounds.size.width/videoView.bounds.size.width
scrollView.minimumZoomScale=宽度比例
scrollView.zoomScale=宽度比例
}
func VIEWFOR缩放(在scrollView:UIScrollView中)->UIView?{
返回视频视图
}
func scrollViewDidZoom(scrollView:UIScrollView){
让videoViewSize=videoView.frame.size
让scrollViewSize=scrollView.bounds.size
让垂直填充=videoViewSize.height
应该做到的是,videoView
应该填满屏幕的宽度并居中,能够放大到它的自然大小。相反,视图看起来居中,但在屏幕上要小得多(可能小3倍)。我做错了什么?Swift5
请试试这个,它会给你你需要的视频大小
let offsetX = max((scrollView.bounds.width - scrollView.contentSize.width) * 0.5, 0)
let offsetY = max((scrollView.bounds.height - scrollView.contentSize.height) * 0.5, 0)
scrollView.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: 0, right: 0)