2011-11-07 73 views
5

我一直在看這段代碼一段時間,仍然困惑爲什麼它不工作。基本上: 1.用戶選擇一個表視圖單元格 2.調用包含高分辨率圖像或UIImage的UIScrollView。 3.以下稱爲Map.m手勢識別器不活動 - UIImageView裏面的UIScrollView

#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 

的姿態沒有得到從拾取的圖像?請幫助

回答

18

UIImageView默認情況下已觸摸&禁用多點觸控交互。要檢測它,你需要自己啓用它。因此,爲了讓您的手勢識別器正常工作,您需要在loadView中添加此項。

試試這個代碼 -

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

這種特殊的動作可以XCode中被啓用。 xib文件。對於此轉到xib其中存在imageView並檢查提及user interaction/multi touch的框。

enter image description here

+0

感謝您的回覆!!!!!然後我把代碼放在調試器中,現在說: –

+0

所以它只是沒有通過縮放來響應它們。它還說: –

+0

這是說什麼? –