调用“ scrollView.zoom”不会缩放

时间:2020-05-05 20:54:36

标签: ios swift uiscrollview uiimageview

由于某些原因,scrollView.zoom无法放大imageView。

我在scrollView中有一个imageView。

ViewDidLoad中:

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

viewForZooming:

   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!)
    }


}

2 个答案:

答案 0 :(得分:0)

我觉得问题出在您的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()

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

答案 1 :(得分:0)

基于评论...

听起来您正在从viewDidLoad()创建/设置您的scrollView及其内容imageView,然后立即尝试对其进行“缩放”。

如果是这样,那就会出现问题,因为自动布局尚未完成对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)
}
相关问题