gpt4 book ai didi

swift - UIScrollView 缩放 & contentInset

转载 作者:IT王子 更新时间:2023-10-29 05:14:11 25 4
gpt4 key购买 nike

类似于用户通过捏合来放大和缩小图像的 iOS 照片应用:

UIView > UIScrollView > UIImageView > UIImage

最初,我遇到缩放比例 1 以下的问题:图像偏离中心。我通过这样做修复了它:

func scrollViewDidZoom(scrollView: UIScrollView) {
let offsetX = max((scrollView.bounds.width - scrollView.contentSize.width) * 0.5, 0)
let offsetY = max((scrollView.bounds.height - scrollView.contentSize.height) * 0.5, 0)
scrollView.contentInset = UIEdgeInsetsMake(offsetY, offsetX, 0, 0)
}

这在缩小时效果很好。

UIImage内容模式是aspectFit

问题

当我ZOOM IN 时,当 zoomScale 大于 1 时, ScrollView 插图需要紧贴 ScrollView 包含的 UIImage 的周围环境。这消除了 UIImage 周围的死空间。 IE、照片应用程序在通过捏合或双击放大时。

尝试过

    func scrollViewDidZoom(scrollView: UIScrollView) {
if scrollView.zoomScale > 1 {
let imageScale = (self.imageView.bounds.width/self.imageView.image!.size.width)
let imageWidth = self.imageView.image!.size.width * imageScale
let imageHeight = self.imageView.image!.size.height * imageScale
scrollView.contentInset = UIEdgeInsetsMake(((scrollView.frame.height - imageHeight) * 0.5), (scrollView.frame.width - imageWidth) * 0.5 , 0, 0)
print (scrollView.contentInset.top)
}
else {

let offsetX = max((scrollView.bounds.width - scrollView.contentSize.width) * 0.5, 0)
let offsetY = max((scrollView.bounds.height - scrollView.contentSize.height) * 0.5, 0)
scrollView.contentInset = UIEdgeInsetsMake(offsetY, offsetX, 0, 0)
}
}

上面的添加似乎仍然改变了插入量。

更新(添加图片)

第一张图片显示了默认布局。放大时显示剩余......

enter image description here

最佳答案

您的方法看起来是正确的。您需要按以下方式更新代码。

    func scrollViewDidZoom(scrollView: UIScrollView) {

if scrollView.zoomScale > 1 {

if let image = imageView.image {

let ratioW = imageView.frame.width / image.size.width
let ratioH = imageView.frame.height / image.size.height

let ratio = ratioW < ratioH ? ratioW:ratioH

let newWidth = image.size.width*ratio
let newHeight = image.size.height*ratio

let left = 0.5 * (newWidth * scrollView.zoomScale > imageView.frame.width ? (newWidth - imageView.frame.width) : (scrollView.frame.width - scrollView.contentSize.width))
let top = 0.5 * (newHeight * scrollView.zoomScale > imageView.frame.height ? (newHeight - imageView.frame.height) : (scrollView.frame.height - scrollView.contentSize.height))

scrollView.contentInset = UIEdgeInsetsMake(top, left, top, left)
}
} else {
scrollView.contentInset = UIEdgeInsetsZero
}
}

swift 5

    func scrollViewDidZoom(scrollView: UIScrollView) {

if scrollView.zoomScale > 1 {

if let image = imageView.image {

let ratioW = imageView.frame.width / image.size.width
let ratioH = imageView.frame.height / image.size.height

let ratio = ratioW < ratioH ? ratioW:ratioH

let newWidth = image.size.width*ratio
let newHeight = image.size.height*ratio

let left = 0.5 * (newWidth * scrollView.zoomScale > imageView.frame.width ? (newWidth - imageView.frame.width) : (scrollView.frame.width - scrollView.contentSize.width))
let top = 0.5 * (newHeight * scrollView.zoomScale > imageView.frame.height ? (newHeight - imageView.frame.height) : (scrollView.frame.height - scrollView.contentSize.height))

scrollView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
}
} else {
scrollView.contentInset = .zero
}
}

关于swift - UIScrollView 缩放 & contentInset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39460256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com