Do I actually need a UIPinchGestureRecognizer inside a UIScrollView to have the pinch working? If yes how do I do it? I am trying to implement what flipboard has, where it basically zooms in an image and have the scroll capability after zooming in. How do I do that?
UPDATE:
Here's some code that I have which doesn't call the scroll view delegate
CGRect imgFrame;
imgFrame.size.width = originalImageSize.width;
imgFrame.size.height = originalImageSize.height;
imgFrame.origin.x = imageOriginPoint.x;
imgFrame.origin.y = imageOriginPoint.y;
NSData *data = [request responseData];
UIImage * image = [UIImage imageWithData:data];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setUserInteractionEnabled:YES];
[imageView setBackgroundColor:[UIColor clearColor]];
[imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[imageView setFrame:CGRectMake(0, 0, imgFrame.size.width, imgFrame.size.height)];
UIScrollView * imgScrollView = [[UIScrollView alloc] initWithFrame:imageView.frame];
imgScrollView.delegate = self;
imgScrollView.showsVerticalScrollIndicator = NO;
imgScrollView.showsHorizontalScrollIndicator = NO;
[imgScrollView setScrollEnabled:YES];
[imgScrollView setClipsToBounds:YES];
[imgScrollView addSubview:imageView];
[imgScrollView setBackgroundColor:[UIColor blueColor]];
[imgScrollView setMaximumZoomScale:1.0];
Swift 2.0 Pinch-zoom Image in Scrollview Steps (Swift 2.0 )
1. Scroll View Constraints
2. Add ImageView In ScrollView and set Constraints
3. Take IBOutlets
4. viewDidLoad Method
5. Scroll View Delegate Method
When I was developing my Zooming PDF viewer in a UIScrollView I found out that when running it on the Phone it actually already has the zooming implemented as part of the phone. But you could have a look at this, http://developer.apple.com/library/ios/#samplecode/ZoomingPDFViewer/Introduction/Intro.html it is a piece of sample code that tries to implement the functionality for zooming, this is want got me started.
I did a custom image viewer not a long ago without the pinch recognizers. Just UIImageView on top of UIScrollView. There you pass a string with a link to the image and it also has a progress bar. Once that image is finished loading the image is shown. Here's the code:
And the header file:
Hope it helps
All you need to do is add your
UIImageView
(or any view you want to zoom) inside yourUIScrollView
.Set your
maximumZoomScale
on yourUIScrollView
to any value higher than 1.0f.Set yourself as the delegate of your
UIScrollView
and return theUIImageView
in the viewForZooming delegate method.That's it. No pinch gesture needed, no nothing.
UIScrollView
handles pinch zooming for you.I guess this answer may be unnecessary but I have had similar problems as @adit and solved it in a very simple way (I think) and maybe someone with similar challenges can use it:
My problem was a very simple I had forgot to add
self.myScrollView.delegate = self;
, which was the reason why i had problems. It took me forever to figure that simple problem out, i guess I did not see the forrest for all the trees :-)