滚动视图无法放大或缩小 swift

Scroll view not being able to zoom in or out in swift

我试图让我的图像能够在页面视图控制器内放大和缩小。

层数:

--UIPageViewController

----CustomPageContentController

------滚动视图

--------UIImageView

------------UIImage

图像显示正常,但我无法通过捏合来放大或缩小。

如何通过 swift 中的代码启用此操作?

这是我目前拥有的:

class PublicationPageView: UIViewController, UIScrollViewDelegate{

    var pageIndex:Int = 0
    var publicationImageView:UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        println("pageIndex: \(pageIndex)")
        publicationImageView.contentMode = UIViewContentMode.ScaleAspectFit


        // Need to reposition the image
        publicationImageView.frame = CGRectMake(0, 0, view.frame.width, view.frame.height-65-65)
        println(publicationImageView.bounds.size)


        var scrollView = UIScrollView(frame: CGRectMake(0, 0, view.frame.width, view.frame.height-65-65))

        scrollView.addSubview(publicationImageView)
        scrollView.contentMode = UIViewContentMode.ScaleAspectFit
        scrollView.maximumZoomScale = 4.0
        scrollView.minimumZoomScale = 1.0

        self.view.addSubview(scrollView)



    }

}

这是最佳实践方式吗?还是会有更好的方法来做到这一点?我愿意接受建议。

提前致谢

您需要实现一个 UIScrollViewDelegate 方法

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

给你 return 考虑缩放的 UIView。

我花了很多时间调试这个和阅读 SO 答案。这是一个问题:在 swift 3 中,它现在被称为:

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

如果您使用旧名称,它不仅可以正常工作而且不会给您任何错误。