gpt4 book ai didi

ios - 放大 UIScrollView 时未调用 scrollViewDidEndZooming

转载 作者:搜寻专家 更新时间:2023-10-31 22:59:29 24 4
gpt4 key购买 nike

这可能是 Xcode 错误?我在 UIScrollView 中有一个 UIImage 在代码中以编程方式完成。用户可以使用缩放手势或双击图像来放大图像。这部分一切正常。

接下来,我尝试使用以下函数 scrollViewDidEndZooming 检测 ScrollView 何时在缩放手势或双击后完成缩放。但是,scrollViewDidEndZooming 永远不会被调用,也不会打印行 I just finished zooming!

代码无效:

func scrollViewDidEndZooming(scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
print("I just finished zooming!")
}

Question:

What might be going on here, why isn't scrollViewDidEndZooming(...) getting called?

注意:我引用了以下文档但没有成功。 https://developer.apple.com/reference/uikit/uiscrollviewdelegate/1619407-scrollviewdidendzooming


完整代码:

class ScrollViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var imageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

imageView = UIImageView(image: UIImage(named: "image.png"))

scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.blackColor()
scrollView.contentSize = imageView.bounds.size
scrollView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]

scrollView.contentOffset = CGPoint(x: 1000, y: 450)

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

scrollView.delegate = self
setZoomScale()
setupGestureRecognizer()
}

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

override func viewWillLayoutSubviews() {
setZoomScale()
}

func setZoomScale() {
let imageViewSize = imageView.bounds.size
let scrollViewSize = scrollView.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height

scrollView.minimumZoomScale = min(widthScale, heightScale)
scrollView.zoomScale = 1.0
}

func scrollViewDidZoom(scrollView: UIScrollView) {
let imageViewSize = imageView.frame.size
let scrollViewSize = scrollView.bounds.size
let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0

scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}

func scrollViewDidEndZooming(scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
print("I just finished zooming!")
}

func setupGestureRecognizer() {
let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
doubleTap.numberOfTapsRequired = 2
scrollView.addGestureRecognizer(doubleTap)
}

func handleDoubleTap(recognizer: UITapGestureRecognizer) {
if (scrollView.zoomScale > scrollView.minimumZoomScale) {
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
} else {
scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
}
}
}

最佳答案

你犯了一个小错误。试试这个代码:

    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
print("I just finished zooming!")
}

关于ios - 放大 UIScrollView 时未调用 scrollViewDidEndZooming,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38923296/

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