Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gesture recognizer is not active - UIImageView inside a UIScrollView

I have been looking at this code for a while and am still puzzled why it is not working. Basically: 1. The user selects a table view cell 2. UIScrollView is invoke that contains a high res image or UIImage. 3. The following is called Map.m

#import "Map.h"

#define ZOOM_VIEW_TAG 100
#define ZOOM_STEP 1.5


@interface Map (UtilityMethods)
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center;
@end


@implementation Map

@synthesize imageScrollView, imageView;

- (void)loadView {
    NSLog(@"beginning of loadView in map.m");
    [super loadView];

    // set the tag for the image view
    [imageView setTag:ZOOM_VIEW_TAG];
    imageScrollView.scrollEnabled = NO;

    // add gesture recognizers to the image view
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];

    [doubleTap setNumberOfTapsRequired:2];
    [twoFingerTap setNumberOfTouchesRequired:2];

    [imageView addGestureRecognizer:singleTap];
    [imageView addGestureRecognizer:doubleTap];
    [imageView addGestureRecognizer:twoFingerTap];

    [singleTap release];
    [doubleTap release];
    [twoFingerTap release];

    // calculate minimum scale to perfectly fit image width, and begin at that scale
    float minimumScale = [imageScrollView frame].size.width  / [imageView frame].size.width;
    [imageScrollView setMinimumZoomScale:minimumScale];
    [imageScrollView setZoomScale:minimumScale];
}


- (void)viewDidUnload {
    self.imageScrollView = nil;
    self.imageView = nil;
}


- (void)dealloc {
    [imageScrollView release];
    [imageView release];
    [super dealloc];
}

#pragma mark UIScrollViewDelegate methods

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [imageScrollView viewWithTag:ZOOM_VIEW_TAG];
}

/************************************** NOTE **************************************/
/* The following delegate method works around a known bug in zoomToRect:animated: */
/* In the next release after 3.0 this workaround will no longer be necessary      */
/**********************************************************************************/
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    [scrollView setZoomScale:scale+0.01 animated:NO];
    [scrollView setZoomScale:scale animated:NO];
}

#pragma mark TapDetectingImageViewDelegate methods

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
    // single tap does nothing for now
}

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
    // double tap zooms in
    NSLog(@"beginning handleDoubleTap to zoom");
    float newScale = [imageScrollView zoomScale] * ZOOM_STEP;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
    [imageScrollView zoomToRect:zoomRect animated:YES];
}

- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer {
    // two-finger tap zooms out
    float newScale = [imageScrollView zoomScale] / ZOOM_STEP;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
    [imageScrollView zoomToRect:zoomRect animated:YES];
}

#pragma mark Utility methods

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {

    CGRect zoomRect;

    // the zoom rect is in the content view's coordinates. 
    //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [imageScrollView frame].size.height / scale;
    zoomRect.size.width  = [imageScrollView frame].size.width  / scale;

    // choose an origin so as to get the right center.
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);

    return zoomRect;
}

@end

The gestures are not getting picked up from the image? Please help

like image 939
Melanie Ann Tarr Avatar asked Nov 07 '11 10:11

Melanie Ann Tarr


1 Answers

UIImageView by default have touch & multi-touch interaction disabled. To detect touches on it you need to enable it yourself. So for your gesture recognizers to work you need to add this in loadView.

Try this in code -

[imageView setUserInteractionEnabled:YES];
[imageView setMultipleTouchEnabled:YES];

This particular action can be enabled in XCode .xib file. For this goto that xib where the imageView is present and check the boxes which mention user interaction / multi touch.

enter image description here

like image 186
Srikar Appalaraju Avatar answered Oct 11 '22 21:10

Srikar Appalaraju