我想在UIImageView中放大ScrollView,但是我不知道该怎么做。

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    var scrollView: UIScrollView = UIScrollView()
    var viewInScrollView: UIView = UIView()
    var selectedImageView: UIImageView = UIImageView()


    override func viewDidLoad() {
        super.viewDidLoad()

        scrollView.delegate = self

        view.addSubview(scrollView)
        scrollView.addSubview(viewInScrollView)
        viewInScrollView.addSubview(selectedImageView)
        selectedImageView.image = UIImage(named: "michael")

        scrollView.translatesAutoresizingMaskIntoConstraints = false
        viewInScrollView.translatesAutoresizingMaskIntoConstraints = false
        selectedImageView.translatesAutoresizingMaskIntoConstraints = false

        scrollView.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor)
        viewInScrollView.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor)
        viewInScrollView.widthAnchor.constraint(equalToConstant: view.frame.width).isActive = true
        viewInScrollView.heightAnchor.constraint(equalToConstant: view.frame.height).isActive = true

        selectedImageView.anchor(top: viewInScrollView.topAnchor, leading: viewInScrollView.leadingAnchor, bottom: viewInScrollView.bottomAnchor, trailing: viewInScrollView.trailingAnchor)

    }

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

}

最佳答案

您必须将maximumZoomScaleminimumZoomScale添加到您的scrollView中,如下所示。

scrollView.minimumZoomScale = 0.25
scrollView.maximumZoomScale = 5.0

希望有帮助!

关于ios - 无法在UIScrollView中缩放UIImageView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53909513/

10-11 22:46