缩放不在滚动视图中工作

时间:2017-03-02 23:07:34

标签: ios xcode swift3

我在swift 3中有一个scrollview。缩放不起作用。我已经设置了scrollview并在其中放置了一个imageview。我已将self设置为委托并实现了viewForZoomingInScrollView。请看一下。感谢。

编辑:我之前没有提到的另一件事是这是导航控制器中的视图。会影响它吗?

class FlowDiagramViewController: UIViewController, UIScrollViewDelegate {

    var scrollView: UIScrollView!
    var imageView:UIImageView! = UIImageView()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let image = UIImage(named: "flowNewRotated1242x2208.png")
        imageView = UIImageView(image: image)

        setupPictureWithZoom()
    }

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

    func setupPictureWithZoom() {
        // http://stackoverflow.com/questions/30014241/uiimageview-pinch-zoom-swift

        imageView.frame = CGRect(x: 0, y: 0, width: 1242, height: 2208)
        // imageView.transform = imageView.transform.rotated(by: CGFloat(M_PI_2))
        imageView!.layer.cornerRadius = 11.0
        imageView!.clipsToBounds = false

        scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))


        scrollView.minimumZoomScale = 0.25
        scrollView.maximumZoomScale = 5.0

        scrollView.delegate = self

        scrollView.backgroundColor = UIColor(red: 90, green: 90, blue: 90, alpha: 0.90)
        scrollView.alwaysBounceVertical = false
        scrollView.alwaysBounceHorizontal = false
        scrollView.showsVerticalScrollIndicator = true
        scrollView.flashScrollIndicators()
        scrollView.contentSize = CGSize(width: 1242, height: 2208)


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


        scrollView.setZoomScale(0.25, animated: true)
    }

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

我已尝试在故事板和代码中定义scrollview。

3 个答案:

答案 0 :(得分:5)

这是一个迅速的问题。

新功能原型如下:

Swift 3版本

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

Swift 2版本

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

答案 1 :(得分:2)

这两行必须在viewDidLoad中:

scrollView.minimumZoomScale = 0.25 
scrollView.maximumZoomScale = 5.0

答案 2 :(得分:0)

尝试更改内容sizer,如下所示,它就像魅力一样

scrollView.contentSize = imageView.frame.size