I am going through the Sample code of iPhone WWDC 2010 - 104 PhotoScroller App. It's working great with my project related images (PDF Page Images)
but I am struggling detect touches in the PhotoScroller App. The Zooming using multiple touches is handled by the ScrollVoiew. Now I want to Zoom In/out the photo on double Tap. The Touchesbegan method is being called in TilingView Class. Then I used [super touchesbegan: withevent:] and now the touches are in the ImageScrollView Class.
How to get these touch events in PhotoViewController. How to achieve the zoom in and zoom out on touch ?
Can anyone help in this Regard ?
I researched several different web sites and I came up with the following...
Place this code into your viewDidLoad or viewWillAppear method:
////////////////////////////// // Listen for Double Tap Zoom UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)]; [doubleTap setNumberOfTapsRequired:2]; [self.scrollView addGestureRecognizer:doubleTap]; [doubleTap release];
Add this to your header file:
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer;
Add this to your implementation:
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer { if(self.scrollView.zoomScale > self.scrollView.minimumZoomScale) [self.scrollView setZoomScale:self.scrollView.minimumZoomScale animated:YES]; else [self.scrollView setZoomScale:self.scrollView.maximumZoomScale animated:YES]; }
Currently this does not center upon the area where the user double tapped.
My code, based upon some of the code at link "UIImageView does not zoom" This code handles toggling between zoomed in and zoomed out and will allow detection of a single tap along with a double tap. It also properly centers the zoom on the embedded image by applying the view transform on it. This code would go in the ImageScrollView class from the sample code.
- (void)setupGestureRecognisers:(UIView *)viewToAttach { UITapGestureRecognizer *dblRecognizer; dblRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapFrom:)]; [dblRecognizer setNumberOfTapsRequired:2]; [viewToAttach addGestureRecognizer:dblRecognizer]; self.doubleTapRecognizer = dblRecognizer; UITapGestureRecognizer *recognizer; recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)]; [recognizer requireGestureRecognizerToFail:dblRecognizer]; [viewToAttach addGestureRecognizer:recognizer]; self.tapRecognizer = recognizer; } - (void)handleTapFrom:(UITapGestureRecognizer *)recognizer { // do your single tap } - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center { CGRect zoomRect; zoomRect.size.height = [_imageView frame].size.height / scale; zoomRect.size.width = [_imageView frame].size.width / scale; center = [_imageView convertPoint:center fromView:self]; zoomRect.origin.x = center.x - ((zoomRect.size.width / 2.0)); zoomRect.origin.y = center.y - ((zoomRect.size.height / 2.0)); return zoomRect; } - (void)handleDoubleTapFrom:(UITapGestureRecognizer *)recognizer { float newScale = [self zoomScale] * 4.0; if (self.zoomScale > self.minimumZoomScale) { [self setZoomScale:self.minimumZoomScale animated:YES]; } else { CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[recognizer locationInView:recognizer.view]]; [self zoomToRect:zoomRect animated:YES]; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With