iOS中的可缩放图像

时间:2019-11-21 14:03:31

标签: ios swift uiscrollview uiimageview zoom

我正在做Apple的官方iOS开发教程。有一项任务是在scrollView中制作可缩放的图像。 我相信这样做是对的。有误,因为我无法放大图像。 任务在下面的屏幕截图中。

Task part 1 Task part 2

这是我的代码:

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        //set the scroll view's deligate to be the viewcontroller instance
        self.scrollView.delegate = self
        viewForZooming(in: scrollView)
        }
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return imageView
    }

    override func viewDidAppear(_ animated: Bool) {
        updateZoomFor(size: view.bounds.size)
    }

    func updateZoomFor(size: CGSize) {
        let widthScale = size.width / imageView.bounds.width
        let heightScale = size.height / imageView.bounds.height
        let scale = min(widthScale, heightScale)
        scrollView.minimumZoomScale = scale
        scrollView.zoomScale = scale
    }
}

0 个答案:

没有答案