我遇到了捏縮放問題UIImageView
它設置在UIScrollView
。圖像會根據用戶的捏點輸入進行縮放,但圖像會變大並變大,直到佔據整個屏幕,而不是停留在我的UIScrollView
的範圍內。我看了一堆不同的SO問題,文章和YouTube視頻,試圖找到問題的根源,但一直未能找到任何東西。Swift - UIImageView的縮放比UIScrollView的大
下SO問題似乎是鬆散的聯繫,但我還是不能看着他們之後解決我的問題:
- content size of UIScrollView
-
- UIScrollView squashes UIImageView image
- Swift UIImageView Stretched Aspect
- UIImageView pinch zoom swift
我的故事板中創建了我的UIScrollView
,我使用在界面生成器中創建的佈局約束將其固定到位。我在viewDidLoad()
中將滾動視圖的代理設置爲self(並且已將UIScrollViewDelegate
添加到我的課程頂部)。 UIImageView
以編程方式創建。我宣佈圖像視圖在類如下面的頂部:
class VCImageViewer: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var lblImageTitle: UILabel!
@IBOutlet weak var btnBack: UIButton!
@IBOutlet weak var scrollView: UIScrollView!
var imageView:UIImageView = UIImageView() // <-- Image view
我然後在viewDidLoad()
添加圖像視圖作爲我UIScrollView
子視圖與代碼的後續行,叫:
self.scrollView.addSubview(self.imageView)
將圖像視圖添加到滾動視圖後,我將圖像加載到圖像視圖中。我從URL中獲取圖像,並使用以下代碼加載它。再次,這在viewDidLoad()
中被調用。
self.imageView.image = UIImage(data: data)
self.imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: (self.imageView.image?.size)!)
if self.imageView.image != nil {
self.lblError.isHidden = true
self.FormatImageViewerWithImage()
}
這確認圖像已正確加載,隱藏着一個警告標籤,如果它沒有我將顯示,並調用self.FormatImageViewerWithImage()
,低於:
func FormatImageViewerWithImage() {
// Constraints
let constraintImageViewTop:NSLayoutConstraint = NSLayoutConstraint(item: self.imageView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.scrollView, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)
let constraintImageViewBottom:NSLayoutConstraint = NSLayoutConstraint(item: self.imageView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.scrollView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
let constraintImageViewLeft:NSLayoutConstraint = NSLayoutConstraint(item: self.imageView, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self.scrollView, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 0)
let constraintImageViewRight:NSLayoutConstraint = NSLayoutConstraint(item: self.imageView, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: self.scrollView, attribute: NSLayoutAttribute.right, multiplier: 1, constant: 0)
self.view.addConstraints([constraintImageViewTop, constraintImageViewBottom, constraintImageViewLeft, constraintImageViewRight])
self.scrollView.contentSize = self.imageView.image!.size
self.scrollView.clipsToBounds = false
let scrollViewFrame = scrollView.frame
let scaleWidth = scrollViewFrame.size.width/self.scrollView.contentSize.width
let scaleHeight = scrollViewFrame.size.height/self.scrollView.contentSize.height
let minScale = min(scaleWidth, scaleHeight)
self.scrollView.minimumZoomScale = minScale
self.scrollView.maximumZoomScale = 1.0
self.scrollView.zoomScale = minScale
}
我也與實施scrollViewDidZoom
:
func scrollViewDidZoom(_ scrollView: UIScrollView) {
self.CentreScrollViewContents()
}
func CentreScrollViewContents() {
let boundsSize = self.scrollView.bounds.size
var contentsFrame = self.imageView.frame
if contentsFrame.size.width < boundsSize.width {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2
} else {
contentsFrame.origin.x = 0
}
if contentsFrame.size.height < boundsSize.height {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2
} else {
contentsFrame.origin.y = 0
}
self.imageView.frame = contentsFrame
}
和viewForZooming
用:
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.imageView
}
我已經看過以前的問題(包括上面列出的一些問題),建議將代碼添加到viewWillLayoutSubviews()
,但是我還沒有取得任何成功,所以我的viewWillLayoutSubviews()
是空的。
任何人都可以看到我做錯了什麼嗎?
更改this:self.scrollView.clipsToBounds = false此self.scrollView.clipsToBounds = true –
@MohammadBashirSidani Legend!非常感謝你,我正在爲此而撕心裂肺。如果您想將其添加爲答案,我會爲您接受。 –
非常高興它的工作!我將添加它作爲答案,謝謝! –