【问题标题】:Gesture recognizer is not active - UIImageView inside a UIScrollView手势识别器未激活 - UIScrollView 内的 UIImageView
【发布时间】:2011-11-07 10:24:29
【问题描述】:

我一直在查看这段代码,但仍然不明白为什么它不起作用。基本上: 1.用户选择一个表格视图单元格 2. UIScrollView 是包含高分辨率图像或 UIImage 的调用。 3.下面叫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

没有从图像中提取手势?请帮忙

【问题讨论】:

    标签: iphone objective-c ios uiimageview uigesturerecognizer


    【解决方案1】:

    UIImageView 默认禁用触控和多点触控交互。要检测对它的触摸,您需要自己启用它。因此,要让您的手势识别器正常工作,您需要将其添加到 loadView

    在代码中试试这个 -

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

    可以在 XCode .xib 文件中启用此特定操作。为此转到xib 所在的imageView 并选中提及user interaction / multi touch 的框。

    【讨论】:

    • 所以事件只是没有通过缩放来响应它们。它还说:
    • 它检测手势但是它没有缩放图像或对它做任何事情、旋转它等。它还说当我 ctrl 单击图像时:忽略对 [UIPanGestureRecognizer setTranslation:inView:] 的调用手势识别器未激活。
    • 正如你的问题 - uiimageview 上的手势无法识别,我想我已经回答了。如果您有任何其他疑问,我建议您写另一个问题。这样问题和答案就不会混淆
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多