图像无响应双击缩放

我的UIImage没有使用以下代码进行缩放。 然后识别轻敲,但缩放不起作用。 它基本上是来自iOS开发站点的taptozoom代码,其中一个导航控制器浮动在UIScrollView和UIImageView后面。 此TapToZoom代码无需导航控制器即可运行。 图像拉伸是否有问题,以允许顶部的导航栏?

#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 { //imageScrollView.userInteractionEnabled = YES; // [self.imageView setUserInteractionEnabled:YES]; //[self.imageView setMultipleTouchEnabled:YES]; 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]; //added self to fix subview [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 NSLog(@"single tap detected"); } - (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 UIScroll Subview #pragma mark Utility methods - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center { CGRect zoomRect; NSLog(@"zoomRectForScale"); // 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); NSLog(@"end of zoom rect"); return zoomRect; } @end 

默认情况下,ImageViews默认不响应用户交互,你必须设置它:

 imageView.userInteractionEnabled = YES; 

你需要确保单击识别器等待双击失败。

 [singleTap requireGestureRecognizerToFail:doubleTap]; 

必须重写-loadView才能初始化self.view – 没有别的。 因为我看不到初始化你的self.view的代码我假设你的视图来自一个nib文件,因此你不能覆盖-loadView方法,或者如果超类负责这个初始化,你不必覆盖它并调用[super loadView] ,但你放在那里的代码完全在错误的地方。 将所有逻辑移到viewDidLoad除了以下行:

 // 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]; 

这些放在viewWillAppear: ,因为这是已正确设置视图层次结构几何的位置,并且您可以拥有准确的frame属性。

导入“RootViewController.h”

定义ZOOM_VIEW_TAG 100

定义ZOOM_STEP 1.5

@interface RootViewController(UtilityMethods) – (CGRect)zoomRectForScale 🙁 float)scale withCenter:(CGPoint)center; @结束

@implementation RootViewController

@synthesize imageScrollView,imageView,mySlider;

  • (IBAction)sliderValueChanged:(UISlider *)sender {mySlider.value = [NSString stringWithFormat:imageScrollView。[sender value]];

    //mySlider.value = [NSString stringWithFormat:@“%。1f”,[发送者值]];

    //myTextField.text = [NSString stringWithFormat:@“%。1f”,[发送者值]]; }

  • (void)loadView {[super loadView];

    //设置图像视图的标签[imageView setTag:ZOOM_VIEW_TAG];

    //向图像视图添加手势识别器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发布]; [doubleTap发布]; [twoFingerTap发布];

    //计算最小比例以完全适合图像宽度,并从该比例开始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发布]; [super dealloc]; }

编译指示标记UIScrollViewDelegate方法

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

/ * ** * ** * ** * ** * ** * ** * ** ** * ** * ** * ** * ** * ** * * / / *以下委托方法有效围绕zoomToRect中的已知错误:动画: / /在3.0之后的下一个版本中,将不再需要此解决方法/ / ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * / – (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {[ scrollView setZoomScale:scale + 0.01 animated:NO]; [scrollView setZoomScale:scale animated:NO]; }

pragma标记TapDetectingImageViewDelegate方法

  • (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {//单击现在什么都不做}

  • (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {//双击放大浮动newScale = [imageScrollView zoomScale] * ZOOM_STEP; CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]]; [imageScrollView zoomToRect:zoomRect animated:YES]; }

  • (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer {//双指点击缩小浮动newScale = [imageScrollView zoomScale] / ZOOM_STEP; CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]]; [imageScrollView zoomToRect:zoomRect animated:YES]; }

pragma mark实用方法

  • (CGRect)zoomRectForScale 🙁 float)scale withCenter:(CGPoint)center {

    CGRect zoomRect;

    //缩放矩形位于内容视图的坐标中。 //在缩放比例为1.0时,它将是imageScrollView边界的大小。 //随着缩放比例的减小,可见的内容越多,矩形的大小就越大。 zoomRect.size.height = [imageScrollView frame] .size.height / scale; zoomRect.size.width = [imageScrollView frame] .size.width / scale;

    //选择原点以获得正确的中心。 zoomRect.origin.x = center.x – (zoomRect.size.width / 2.0); zoomRect.origin.y = center.y – (zoomRect.size.height / 2.0);

    return zoomRect; }

@结束