gpt4 book ai didi

ios - 为什么 UIScrollView 的内容不居中?

转载 作者:行者123 更新时间:2023-11-28 11:38:40 24 4
gpt4 key购买 nike

我在 UIContentView 中嵌入了一个 UIImageView 以使其可缩放和平移 ( reference 。问题是图像没有正确居中。它看起来像整个图像内容移出了左上角: enter image description here

虽然打印的内容偏移量接近于零:内容偏移量为:(0.0, -20.0)

这是我的实现:

import UIKit

class ZoomableImageView: UIScrollView {

private struct Constants {
static let minimumZoomScale: CGFloat = 0.5;
static let maximumZoomScale: CGFloat = 6.0;
}


// public so that delegate can access
public let imageView = UIImageView()

// gw: must be called to complete a setting
public func setImage(image: UIImage) {
imageView.image = image
imageView.bounds = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)

self.contentSize = image.size


let scaleFitZoomScale: CGFloat = min(
self.frame.width / image.size.width ,
self.frame.height / image.size.height
)


// reset scale and offset on each resetting of image
self.zoomScale = scaleFitZoomScale
}


// MARK - constructor
init() {
// gw: there is no super.init(), you have to use this constructor as hack
super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0)) // gw: relies on autolayout constraint later

minimumZoomScale = Constants.minimumZoomScale
maximumZoomScale = Constants.maximumZoomScale

addSubview(imageView)


}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}



}

View Controller :

class ViewController: UIViewController, UIScrollViewDelegate {


let zoomableImageView = ZoomableImageView()


override func viewDidLoad() {
super.viewDidLoad()

view.addSubview(zoomableImageView)
zoomableImageView.delegate = self

}

override func viewDidLayoutSubviews() {

zoomableImageView.frame = view.frame
let image = UIImage(imageLiteralResourceName: "kelly")
zoomableImageView.setImage(image: image)

}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
// print("scale factor is: \(scrollView.zoomScale)")
return zoomableImageView.imageView
}

func scrollViewDidZoom(_ scrollView: UIScrollView) {
print("scale factor is: \(scrollView.zoomScale)")
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("content offset is: \(scrollView.contentOffset)")
}



}

但是,如果我不将 UIImageViewUIScrollView 包装在自定义类下,图像的居中效果会很好:

class ViewController: UIViewController, UIScrollViewDelegate {


let imageView: UIImageView = {
let image = UIImage(imageLiteralResourceName: "kelly")
let imageView = UIImageView(image: image)
imageView.bounds = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
return imageView
} ()

let scrollView: UIScrollView = {
let scrollView = UIScrollView()
return scrollView
} ()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

view.addSubview(scrollView)
scrollView.addSubview(imageView)


scrollView.delegate = self
self.scrollView.minimumZoomScale = 0.5;
self.scrollView.maximumZoomScale = 6.0;

}

override func viewDidLayoutSubviews() {
scrollView.frame = view.frame

if let size = imageView.image?.size {
scrollView.contentSize = size
}



}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}


}

我是否在第一次实现中遗漏了什么?

最佳答案

在你的ZoomableImageView类,您正在设置 bounds 当它应该是 frame

所以改变:

imageView.bounds = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height) 

imageView.frame =  CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)

关于ios - 为什么 UIScrollView 的内容不居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54349162/

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