Search code examples
iosswiftuiscrollviewuiimageview

Pinch Zoom into multiple Images on a single view (Swift)


I'm a little confused on how I can properly scroll into images, when I'm using multiple UIScrollViews

I have

  • pinchScrollView1
  • pinchScrollView2
  • image1
  • image2

    pinchScrollView1.minimumZoomScale=1.0
    pinchScrollView1.maximumZoomScale=6.0
    pinchScrollView2.minimumZoomScale=1.0
    pinchScrollView2.maximumZoomScale=6.0
    
    func viewForZooming(in scrollView: UIScrollView) -> UIView?
    {
    
    return self.image1
    }
    

So this works great for the first image, and I thought perhaps I could check to see which scrollview was focused, then return the corresponding image like

 if pinchZoomScroll1.isFocused {
      return self.image1
 }
 else { return self.image2}

but this of course, is not properly zooming the correct image

I'm thinking it has to do with me setting both UIScrollView's delegates in the storyboard, but i'm a little lost on how to basically have two different "viewForZooming" functions, for each respective ScrollView.

any assistance would be greatly appreciated!


Solution

  • So I ended up figuring it out myself, here's what I did!

    I removed the delegates from the storyboard

     self.pinchZoomScroll1.delegate = self
     self.pinchZoomScroll2.delegate = self
     pinchZoomScroll1.tag = 1
     pinchZoomScroll2.tag = 2
    
      func viewForZooming(in scrollView: UIScrollView) -> UIView?
      {
        if scrollView.tag == 1 {
           return self.image1
        }
        else {
           return self.image2
        }
    
    }
    

    Feel free to let me know if there's a better approach, but it's working! Hope this helps someone else.