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

发布于 2024-10-30 11:41:05 字数 1143 浏览 4 评论 0原文

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

这是标题:

#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

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

I've created a small sample project with a nib containing an image view. In my view controller code, I've added a gesture recogniser to the image view to detect taps. But it never calls the handler method.

Here's the header:

#import <UIKit/UIKit.h>

@interface TapExperimentViewController : UIViewController {
    UIImageView *imageView;
}

@property (retain) IBOutlet UIImageView *imageView;

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

@end

And here's the implementation file:

#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

I've made sure that the outlet is connected. Why isn't handleTap: being called when I touch the image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无名指的心愿 2024-11-06 11:41:05

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

配置了新的图像视图对象
默认情况下忽略用户事件。
如果你想处理a中的事件
UIImageView 的自定义子类,您
必须显式改变的值
userInteractionEnabled 属性
初始化对象后是的。

I have not worked with gestures myself but I know that UIImageViews do not enable user interaction by default.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文