IOS: add imageview in a scrollview to have zoom

cyclingIsBetter picture cyclingIsBetter · Jul 3, 2012 · Viewed 42.2k times · Source

I want to set a UIImageView with a UIImage and put this imageview inside a UIScrollView to obtain a zoom of this image; and I want this UIImageView and UIScrollView to fit in the rect at the center of the view...is it possible?

Answer

Justin Paulson picture Justin Paulson · Jul 3, 2012
  1. Set your view controller up as a <UIScrollViewDelegate>
  2. Draw your UIScrollView the size you want for the rectangle at the center of the view. Set the max zoom in the inspector to something bigger than 1. Like 4 or 10.
  3. Right click on the scroll view and connect the delegate to your view controller.
  4. Draw your UIImageView in the UIScrollView and set it up with whatever image you want. Make it the same size as the UIScrollView.
  5. Ctrl + drag form you UIImageView to the .h of your View controller to create an IBOutlet for the UIImageView, call it something clever like imageView.
  6. Add this code:

    -(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
    {
        return self.imageView;
    }
    
  7. Run the app and pinch and pan til your heart's content.