为什么我的点击未被识别?
我创建了一个小型示例项目,其中的笔尖包含图像视图。在我的视图控制器代码中,我在图像视图中添加了一个手势识别器来检测点击。但它从不调用处理程序方法。
这是标题:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己没有使用过手势,但我知道
UIImageView
默认情况下不启用用户交互。I have not worked with gestures myself but I know that
UIImageView
s do not enable user interaction by default.