【发布时间】:2017-03-14 21:41:18
【问题描述】:
我有一个AVPlayerViewController,它的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)
}
应该发生的是videoView 应该填充屏幕的宽度并居中,并能够放大到其自然大小。相反,视图在屏幕上显示为居中但小得多(可能小 3 倍)。我做错了什么?
【问题讨论】:
标签: ios swift uiscrollview