TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

[SOLVED]UIScrollView not scrolling inside a Root UIScrollView with UIKit

Forums > iOS

Hi Folks,

I'm trying to zoom an image that is inside a UIScrollView, and that UIScrollView it's inside a root UIScrollView that allow paging, similar to the Photos app.

In the viewDidLoad() inside the view that contains the root scrollView I call 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()

}

The loadPages() function load every scrollView with their image and add it to the root scrollview

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)
    }
}

The Page object it's just a UIScrollView object`

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

And inside the root scrollView viewForZooming function it calls the viewForZooming function of the correspond child scrollview.

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
}

With this approach I can navigate horizontally but I can't zoom in the current image. The viewForZooming function executes in both objects. What I'm doing wrong? Is this a correct approach? For me it seems a bit confusing but I wasn´t able to find any tutorial that has this action to zoom an image that it´s inside a root scrollView.

In StackOverflow a person comments me to disable the zoom in the root scrollView but it doesn't work.

Thank you in advance.

3      

I finally resolved my issue. I post it as an answer in Stack Overflow

3      

Hey @Flaviusns, I tried looking at your solution but i'm still having inconsistent behaviour when zooming in and moving the image around. Do you mind pasting a sample of your working code?

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.