iphone - 为什么我的点击未被识别?

标签 iphone ios uigesturerecognizer

我创建了一个小型示例项目,其中的 Nib 包含 ImageView 。在我的 View Controller 代码中,我在 ImageView 中添加了一个手势识别器来检测点击。但它从不调用处理程序方法。

这是标题:

#import <UIKit/UIKit.h>

@interface TapExperimentViewController : UIViewController {
    UIImageView *imageView;
}

@property (retain) IBOutlet UIImageView *imageView;

- (void)handleTap:(UIGestureRecognizer *)sender;

@end

这是实现文件:

#import "TapExperimentViewController.h"

@implementation TapExperimentViewController

@synthesize imageView;

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

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(handleTap:)];
    [self.imageView addGestureRecognizer:tap];
    [tap release];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    self.imageView = nil;
}

- (void)handleTap:(UIGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"tap");
    }
}

@end

我已确保 socket 已连接。为什么当我触摸图像时,handleTap: 没有被调用?

最佳答案

我自己没有使用过手势,但我知道UIImageView默认情况下不启用用户交互。

New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.

关于iphone - 为什么我的点击未被识别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5557411/

相关文章:

ios - TouchBegan 在 iOS 12 中被调用,但在 iOS 13 中未被调用

iphone - iPhone 应用程序中网页 View 上的手势识别器

iPhone:NSTimer

ios - 丰富的推送通知在 iOS 的 iPhone 11 Pro Max 中不起作用

Swift textFieldDidChangeSelection 在 ios 12 中没有被调用

ios - Xcode 编辑器在编译时花费太多时间

ios - 线程池 : DispatchQueue. main.async

ios - 在 ios 中使用 LongGestureRecognizer 更改 View 选定部分的颜色

iOS:iOS 6 上的 NSXMLParser 错误(代码 100)

iphone - 使用 Corebluetooth 框架检测耳机