Swift - UIImageView 比 UIScrollView 缩放得更大



我在捏缩放设置在UIScrollView内的UIImageView时遇到问题。 图像会根据用户的捏合输入进行缩放,但图像会缩放并变大,直到占据整个屏幕,而不是停留在我的UIScrollView范围内。 我查看了一堆不同的SO问题,文章和YouTube视频,试图找到问题的根源,但找不到任何东西。

以下 SO 问题似乎松散相关,但看完后我仍然无法解决我的问题:

  • UIScrollView 的内容大小
  • 使用自动布局的缩放 UIImageView 的 UIScrollView
  • UIScrollView squashes UIImageView image
  • Swift UIImageView Stretched Aspect
  • UIImageView捏缩放快速

我的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

然后,我使用以下代码行将图像视图添加为UIScrollView的子视图,在viewDidLoad()期间调用:

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()是空的。

谁能看到我做错了什么?

请更改此行:

self.scrollView.clipsToBounds = false

到这一行:

self.scrollView.clipsToBounds = true

希望这有帮助!

最新更新