2017-08-15 112 views
1

我遇到了捏縮放問題UIImageView它設置在UIScrollView。圖像會根據用戶的捏點輸入進行縮放,但圖像會變大並變大,直到佔據整個屏幕,而不是停留在我的UIScrollView的範圍內。我看了一堆不同的SO問題,文章和YouTube視頻,試圖找到問題的根源,但一直未能找到任何東西。Swift - UIImageView的縮放比UIScrollView的大

下SO問題似乎是鬆散的聯繫,但我還是不能看着他們之後解決我的問題:

我的故事板中創建了我的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()是空的。

任何人都可以看到我做錯了什麼嗎?

+1

更改this:self.scrollView.clipsToBounds = false此self.scrollView.clipsToBounds = true –

+0

@MohammadBashirSidani Legend!非常感謝你,我正在爲此而撕心裂肺。如果您想將其添加爲答案,我會爲您接受。 –

+0

非常高興它的工作!我將添加它作爲答案,謝謝! –

回答

1

請改變這一行:

self.scrollView.clipsToBounds = false 

這一行:

self.scrollView.clipsToBounds = true 

希望這有助於!

相關問題