gpt4 book ai didi

ios - 'CALayer 位置包含 NaN : [nan nan]' on UIScrollView

转载 作者:行者123 更新时间:2023-11-30 10:58:15 35 4
gpt4 key购买 nike

我正在开发一个屏幕,其中 UIScrollView 内只有一个 UIImageViewUIScrollView 使用户能够固定和缩放图像。我从下面的帖子中得到了帮助。它使用 Storyboard和自动布局。基本上,它有 8 个约束,分别是顶部、底部、前导和尾随(scrollview 到 superview 和 imageView 到scrollView)。示例帖子在 Storyboard内设置图像,以便不需要给出任何特定的高度或宽度。

https://www.raywenderlich.com/560-uiscrollview-tutorial-getting-started

我的问题是,当我使用相同的代码但以编程方式执行所有操作时,我可以看到图像(额外缩放)并且我可以用一根手指滚动,但当我想捏它时(缩小、放大)我得到了

'CALayer position contains NaN: [nan nan]'

错误。我认为这是因为 ScrollView 没有设置 contentSize 或 contentOffset 但无论我设置这些属性,我都会遇到相同的错误。

 override func viewDidLoad() {

self.scrollView.delegate = self
self.scrollView.translatesAutoresizingMaskIntoConstraints = false
self.imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = photo
imageView.contentMode = .scaleAspectFit
scrollView.addSubview(imageView)
self.view.addSubview(scrollView)
self.imageView.setContentHuggingPriority(UILayoutPriority.init(251), for: NSLayoutConstraint.Axis.vertical)
self.imageView.setContentHuggingPriority(UILayoutPriority.init(251), for: NSLayoutConstraint.Axis.horizontal)

scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

imageViewTopConstraint = imageView.topAnchor.constraint(equalTo: self.scrollView.topAnchor, constant: 0)
imageViewTopConstraint.isActive = true

imageViewBottomConstraint = imageView.bottomAnchor.constraint(equalTo: self.scrollView.bottomAnchor, constant: 0)
imageViewBottomConstraint.isActive = true

imageViewLeadingConstraint = imageView.leadingAnchor.constraint(equalTo: self.scrollView.leadingAnchor, constant: 0)
imageViewLeadingConstraint.isActive = true

imageViewTrailingConstraint = imageView.trailingAnchor.constraint(equalTo: self.scrollView.trailingAnchor, constant: 0)
imageViewTrailingConstraint.isActive = true

scrollView.contentSize = imageView.bounds.size

}

fileprivate func updateMinZoomScaleForSize(_ size: CGSize) {
let widthScale = size.width / imageView.bounds.width
let heightScale = size.height / imageView.bounds.height
let minScale = min(widthScale, heightScale)

scrollView.minimumZoomScale = minScale

scrollView.zoomScale = minScale
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

updateMinZoomScaleForSize(view.bounds.size)
}

fileprivate func updateConstraintsForSize(_ size: CGSize) {

let yOffset = max(0, (size.height - imageView.frame.height) / 2)
imageViewTopConstraint.constant = yOffset
imageViewBottomConstraint.constant = yOffset

let xOffset = max(0, (size.width - imageView.frame.width) / 2)
imageViewLeadingConstraint.constant = xOffset
imageViewTrailingConstraint.constant = xOffset

view.layoutIfNeeded()
}

}

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

func scrollViewDidZoom(_ scrollView: UIScrollView) {
updateConstraintsForSize(view.bounds.size)
}

}

最佳答案

我通过将 updateMinZoomScaleForSize(view.bounds.size) 函数从 ViewDidLayoutSubViews() 移动到 ViewDidLoad() 并调用 self.view.layoutIfNeeded() 在调用 ZoomScale 函数之前。因此我的最终和工作 ViewDidLoad()

    override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.delegate = self
self.scrollView.translatesAutoresizingMaskIntoConstraints = false
self.imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = photo
imageView.contentMode = .scaleAspectFit
imageView.clipsToBounds = true
scrollView.addSubview(imageView)
//scrollView.contentSize = CGSize(width: imageView.image!.size.width, height: imageView.image!.size.height)
self.view.addSubview(scrollView)
self.imageView.setContentHuggingPriority(UILayoutPriority.init(251), for: NSLayoutConstraint.Axis.vertical)
self.imageView.setContentHuggingPriority(UILayoutPriority.init(251), for: NSLayoutConstraint.Axis.horizontal)

scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

imageViewTopConstraint = imageView.topAnchor.constraint(equalTo: self.scrollView.topAnchor, constant: 0)
imageViewTopConstraint.isActive = true

imageViewBottomConstraint = imageView.bottomAnchor.constraint(equalTo: self.scrollView.bottomAnchor, constant: 0)
imageViewBottomConstraint.isActive = true

imageViewLeadingConstraint = imageView.leadingAnchor.constraint(equalTo: self.scrollView.leadingAnchor, constant: 0)
imageViewLeadingConstraint.isActive = true

imageViewTrailingConstraint = imageView.trailingAnchor.constraint(equalTo: self.scrollView.trailingAnchor, constant: 0)
imageViewTrailingConstraint.isActive = true


self.view.layoutIfNeeded()
updateMinZoomScaleForSize(view.bounds.size)

}

关于ios - 'CALayer 位置包含 NaN : [nan nan]' on UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53706191/

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