UIScrollView不会捏缩放。 (迅速)

时间:2016-02-20 17:53:34

标签: ios swift uiscrollview

我无法让我的UIScrollView进行缩放。我的代码如下:

class ViewController: UIViewController, UIScrollViewDelegate {

    let imageView = UIImageView()

    @IBOutlet weak var scrollView: UIScrollView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let image = UIImage(named: "cats.jpg")
        imageView.image = image
        imageView.frame = CGRect(origin: CGPointZero, size: image!.size)
        scrollView.addSubview(imageView)
        scrollView.contentSize = image!.size
        scrollView.clipsToBounds = false
        scrollView.layer.borderColor = UIColor.yellowColor().CGColor
        scrollView.layer.borderWidth = 4.0

        let scrollViewFrame = scrollView.frame
        let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
        let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
        let minScale = min(scaleWidth, scaleHeight);
        scrollView.minimumZoomScale = minScale;

        scrollView.maximumZoomScale = 1.0
        scrollView.zoomScale = minScale;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


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

}

我在这里做错了什么?我已经尝试调整最小和最大比例,但没有任何区别。对此的任何指示都将非常感激。谢谢!

1 个答案:

答案 0 :(得分:8)

将此添加到viewDidLoad

scrollView.delegate = self

滚动视图需要一个委托(您的视图控制器)来获取缩放视图(您实现的委托方法)。