图像缩放在根 UIScrollView 内的 UIScrollView 中不起作用

Image Zoom not working in a UIScrollView inside a Root UIScrollView

我正在尝试缩放 UIScrollView 内的图像,而 UIScrollView 位于允许分页的根 UIScrollView 内,类似于照片应用程序。

在包含根scrollView的视图里面[​​=14=]我调用prepareScrollView()

    func prepareScrollView(){

      let tap = UITapGestureRecognizer(target: self, action: #selector(hideNavigationItem))


      scrollView.addGestureRecognizer(tap)

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


      let width = self.view.bounds.width
      let height = self.view.bounds.height
      totalWidth = width * CGFloat((imageArray!.count)!) //The width must be the width of the screen by the number of the images in the array
      scrollView.contentSize = CGSize(width: totalWidth, height: height)
      scrollView.isPagingEnabled = true

      loadPages()

}

loadPages() 函数加载每个滚动视图及其图像并将其添加到根滚动视图。

func loadPages(){

    for i in 0...((imageArray!.count)! - 1){

        let pageScroll = Page(frame: CGRect(x: self.view.bounds.size.width * CGFloat(i), y: self.view.frame.origin.y, width: self.view.bounds.size.width, height: self.view.bounds.size.height))

        pageScroll.minimumZoomScale = 1.0
        pageScroll.maximumZoomScale = 4.0
        pageScroll.zoomScale = 1.0

        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height))
        imageView.image = UIImage(data: (imageArray![i])!)
        imageView.contentMode = .scaleToFill
        pageScroll.addSubview(imageView)

        scrollView.addSubview(pageScroll)
    }
}

Page 对象只是一个 UIScrollView 对象。

class Page: UIScrollView {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return self.subviews[0]//Return the image
}

}

并且在根 scrollView viewForZooming 函数中调用相应子滚动视图的 viewForZooming 函数。

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

    if(self.scrollView.subviews.count > 0){
        let childScrollView = self.scrollView.subviews[currentPage] as! Page
        let image = childScrollView.viewForZooming(in: scrollView)
        return image
    }
    return nil
}

通过这种方法,我可以水平导航,但无法放大当前图像。 viewForZooming 函数在两个对象中执行。我做错了什么?

终于解决了我的问题。我将 Page 对象更改为简单的 UIScrollView,然后将 scrollView 对象的委托设置为 self

func loadPages(){

    for i in 0...((comic?.comicsPages!.count)! - 1){

        let pageScroll = UIScrollView(frame: CGRect(x: self.view.bounds.size.width * CGFloat(i), y: self.view.frame.origin.y, width: self.view.bounds.size.width, height: self.view.bounds.size.height))

        pageScroll.minimumZoomScale = 1.0
        pageScroll.maximumZoomScale = 4.0
        pageScroll.zoomScale = 1.0
        pageScroll.isUserInteractionEnabled = true
        pageScroll.delegate = self //Here

        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height))
        imageView.image = UIImage(data: (imageArray![i])!)
        imageView.contentMode = .scaleToFill

        pageScroll.addSubview(imageView)

        scrollView.addSubview(pageScroll)
    }
}

然后在根 scrollView 的 viewForZooming 函数中,我为此更改了代码:

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

    if(scrollView.subviews.count > 0){
        return scrollView.subviews[0]
    }
    return nil
}

scrollView 参数是子滚动视图,因此它按预期工作。