viewWillLayoutSubviews
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
setZoomScaleFor(scrollViewSize: scrollView.bounds.size)
if scrollView.zoomScale < scrollView.minimumZoomScale {
scrollView.zoomScale = scrollView.minimumZoomScale
}
recenterImage()
}
自動(dòng)布局
flexibleLeftMargin 自動(dòng)調(diào)整view與父視圖左邊距徘层,保證右邊距不變 flexibleWidth 自動(dòng)調(diào)整view的寬度氓鄙,保證左邊距和右邊距不變 flexibleRightMargin 自動(dòng)調(diào)整view與父視圖右邊距,以保證左邊距不變 flexibleTopMargin 自動(dòng)調(diào)整view與父視圖上邊距,以保證下邊距不變 flexibleHeight 自動(dòng)調(diào)整view的高度邀跃,以保證上邊距和下邊距不變 flexibleBottomMargin自動(dòng)調(diào)整view與父視圖的下邊距坛猪,以保證上邊距不變
scrollView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
<li><h1>學(xué)習(xí)代碼:<h1></li>
ViewController.swift
import UIKit
let YHRect = UIScreen.main.bounds
let YHHeight = YHRect.size.height
let YHWidth = YHRect.size.width
class ViewController: UIViewController {
let scrollView = UIScrollView(frame: YHRect)
let imageView = UIImageView(image: UIImage(named: "steve"))
override func viewDidLoad() {
super.viewDidLoad()
setupBG()
setupScrollView()
setZoomScaleFor(scrollViewSize: scrollView.bounds.size)
scrollView.zoomScale = scrollView.minimumZoomScale
recenterImage()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
setZoomScaleFor(scrollViewSize: scrollView.bounds.size)
if scrollView.zoomScale < scrollView.minimumZoomScale {
scrollView.zoomScale = scrollView.minimumZoomScale
}
recenterImage()
}
//背景
func setupBG() {
view.layer.contents = UIImage(named: "steve")?.cgImage
let visual = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
visual.frame = YHRect
view.addSubview(visual)
}
//scrollview
func setupScrollView() {
/*自動(dòng)布局
flexibleLeftMargin 自動(dòng)調(diào)整view與父視圖左邊距,保證右邊距不變
flexibleWidth 自動(dòng)調(diào)整view的寬度污秆,保證左邊距和右邊距不變
flexibleRightMargin 自動(dòng)調(diào)整view與父視圖右邊距劈猪,以保證左邊距不變
flexibleTopMargin 自動(dòng)調(diào)整view與父視圖上邊距,以保證下邊距不變
flexibleHeight 自動(dòng)調(diào)整view的高度良拼,以保證上邊距和下邊距不變
flexibleBottomMargin自動(dòng)調(diào)整view與父視圖的下邊距战得,以保證上邊距不變
*/
scrollView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
scrollView.backgroundColor = .clear
scrollView.contentSize = imageView.bounds.size
scrollView.delegate = self
scrollView.addSubview(imageView)
view.addSubview(scrollView)
}
//設(shè)置縮放
func setZoomScaleFor(scrollViewSize: CGSize) {
let imageSize = imageView.bounds.size
let widthScale = scrollViewSize.width/imageSize.width
let heightScale = scrollViewSize.height/imageSize.height
let minimunScale = min(widthScale,heightScale)
scrollView.minimumZoomScale = minimunScale
scrollView.maximumZoomScale = 3.5
}
//重新定位image
func recenterImage() {
let scrollViewSize = scrollView.bounds.size
let imageViewSize = imageView.frame.size
let horizontalSpace = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2.0 : 0
let verticalSpace = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.width) / 2.0 :0
scrollView.contentInset = UIEdgeInsetsMake(verticalSpace, horizontalSpace, verticalSpace, horizontalSpace)
}
}
extension ViewController: UIScrollViewDelegate {
//要縮放時(shí)調(diào)用,返回需要縮放的view
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
//縮放后調(diào)用
func scrollViewDidZoom(_ scrollView: UIScrollView) {
recenterImage()
}
}