调用 "scrollView.zoom "并不能缩放。

问题描述 投票:0回答:2

由于某些原因,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
    }

现在,我在滚动视图和图像视图都被初始化后调用了下面的函数。


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


}

ios swift uiscrollview uiimageview
2个回答
0
投票

根据评论...

听起来你正在创建设置你的scrollView和它的内容imageView从 viewDidLoad() 然后立即尝试 "放大 "它。

如果是这样,那就会出现问题,因为自动布局还没有完成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)
}

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()

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

© www.soinside.com 2019 - 2024. All rights reserved.