调用 'scrollView.zoom' 不缩放

Calling 'scrollView.zoom' Does Not Zoom

出于某种原因 scrollView.zoom 没有放大 imageView。

我在 scrollView 中有一个 imageView。

ViewDidLoad中:

        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 2.0
        scrollView.delegate = self

视图缩放:

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

现在,在 scrollView 和 imageView 都初始化后,我在下面调用。


var scale: CGFloat = 2

let location = CGPoint(x: imageView.frame.width/2, y: imageView.frame.height/2)
scrollView.zoom(to: zoomRectForScale(scale, center: location), animated: true)

调用 scrollView.zoom 后没有任何反应。我试过

view.setNeedsDisplay()
view.layoutIfNeeded()

仍然没有任何反应,imageView 没有放大。

更新:

应要求,这里是scrollView和imageView的初始化代码:

func createscrollView(image: UIImage?){

    if image == nil{
        let img = UIImage(named: "demo image.jpg")
        let imgCorrected = scaleAndOrient(image: img!)

        //created user selecged images
        userSelectedImage_UI = img
        userSelectedImage_UI_Corrected = imgCorrected
    }


    // create image scroll view
    scrollView = UIScrollView(frame: CGRect(x: 0, y: 0,width: 100, height: 100))
    scrollView.showsHorizontalScrollIndicator = false
    scrollView.showsVerticalScrollIndicator = false
    scrollView.bouncesZoom = false
    scrollView.bounces = false
    scrollView.contentMode = .scaleAspectFill

    view.addSubview(scrollView)

    scrollView.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([
        scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0),
        scrollView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0),
        scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -150),
        scrollView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0)
    ])


    // add image view to scrollview
    imageView = UIImageView(frame: CGRect(x: 0, y: 0,width: 100, height: 100))
    scrollView.addSubview(imageView)

    imageView.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([
        imageView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor, constant: 0),
        imageView.leftAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leftAnchor, constant: 0),
        imageView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor, constant: 0),
        imageView.rightAnchor.constraint(equalTo: scrollView.contentLayoutGuide.rightAnchor, constant: 0),
        imageView.widthAnchor.constraint(equalTo: scrollView.widthAnchor, multiplier: 1),
        imageView.heightAnchor.constraint(equalTo: scrollView.heightAnchor, multiplier: 1)
    ])

    imageView.contentMode = .scaleAspectFit

    if image == nil{
        imageView.image = userSelectedImage_UI_Corrected

    } else {
        imageView.image = scaleAndOrient(image: image!)
    }


}

我感觉问题出在您的 zoomRectForScale 函数中,尽管它没有发布。它应该是这样的:

func zoomRectForScale(_ scale: CGFloat, center: CGPoint) -> CGRect {
    var zoomRect = CGRect.zero
    zoomRect.size.height = imageView.frame.size.height / scale
    zoomRect.size.width = imageView.frame.size.width / scale
    let newCenter = scrollView.convert(center, from: imageView)
    zoomRect.origin.x = newCenter.x - (zoomRect.size.width / 2.0)
    zoomRect.origin.y = newCenter.y - (zoomRect.size.height / 2.0)
    return zoomRect
}

此外,我已经使用以下方法验证了双击以缩放到正确的位置:

var gestureRecognizer: UITapGestureRecognizer!

// Sets up the gesture recognizer that receives double taps to auto-zoom
func setupGestureRecognizer() {
    gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
    gestureRecognizer.numberOfTapsRequired = 2
    scrollView.addGestureRecognizer(gestureRecognizer)
}

// Handles a double tap by either resetting the zoom or zooming to where was tapped
@IBAction func handleDoubleTap() {
    if scrollView.zoomScale == 1 {
        scrollView.zoom(to: zoomRectForScale(scrollView.maximumZoomScale, center: gestureRecognizer.location(in: gestureRecognizer.view)), animated: true)
    } else {
        scrollView.setZoomScale(1, animated: true)
    }
}

不要忘记在 viewDidLoad 中调用它:

setupGestureRecognizer()

如果这不能解决您的问题,请使用缺少的代码函数和变量声明来调整您的问题。

根据评论...

听起来您正在从 viewDidLoad() 创建/设置 scrollView 及其内容 imageView,然后立即尝试 "zoom" 它。

如果是这样,那将是有问题的,因为自动布局尚未完成 UI 元素的布局。

可以调用它:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    var scale: CGFloat = 2
    let location = CGPoint(x: imageView.frame.width/2, y: imageView.frame.height/2)
    scrollView.zoom(to: zoomRectForScale(scale, center: location), animated: true)
}