UIScrollView无法缩放:我缺少什么?

时间:2018-12-05 01:17:43

标签: swift xcode uiscrollview

我可以滚动,但不能缩放。我在搜索时看到了很多看起来相同的代码,但是仍然没有缩放。

pinchGestureRecognizer不是nil

viewForZoomingInScrollView(scrollView: UIScrollView)没有被调用。

scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat)报告比例= 1.0

我在这里想念什么?

(我们需要以编程方式而不是在IB中进行此操作。)

import UIKit

class ScrollViewController: UIViewController, UIScrollViewDelegate {

    var scrollView: UIScrollView!
    var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        imageView = UIImageView(image: UIImage(named: "image.png"))

        scrollView = UIScrollView(frame: view.bounds)
        scrollView.backgroundColor = UIColor.black
        scrollView.contentSize = imageView.bounds.size
        scrollView.contentOffset = CGPoint(x: 1000, y: 450)

        scrollView.addSubview(imageView)
        view.addSubview(scrollView)

        scrollView.delegate = self
        scrollView.minimumZoomScale = 0.1
        scrollView.maximumZoomScale = 4.0
        scrollView.zoomScale = 1.0
    }

    //delegate
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }
}

1 个答案:

答案 0 :(得分:2)

您是从旧版Swift转换代码吗?因为返回视图以进行缩放的委托函数现在已经

func viewForZooming(in scrollView: UIScrollView) -> UIView?