iPhone - 如何捕捉 UIWebView 或 UIImageView 的点击
我有一个像这样生成的视图:
oneView = [[UIView alloc] initWithFrame:CGRectMake(x, y , w, h)];
oneView.tag = i;
UITapGestureRecognizer* recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap2:)];
[recognizer setNumberOfTouchesRequired:1];
[oneView addGestureRecognizer:recognizer];
recognizer.delegate = self;
[recognizer release];
- (void)handleSingleTap2:(UITapGestureRecognizer *)recognizer {
int viewID = recognizer.view.tag;
NSLog(@"id : %d", viewID);
}
这有效,但是如果我用 UIWebView 或 UIImageView 分配替换 UIView 分配,则不再触发触摸事件。
我怎样才能让它发挥作用?
I have a View generated like this :
oneView = [[UIView alloc] initWithFrame:CGRectMake(x, y , w, h)];
oneView.tag = i;
UITapGestureRecognizer* recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap2:)];
[recognizer setNumberOfTouchesRequired:1];
[oneView addGestureRecognizer:recognizer];
recognizer.delegate = self;
[recognizer release];
- (void)handleSingleTap2:(UITapGestureRecognizer *)recognizer {
int viewID = recognizer.view.tag;
NSLog(@"id : %d", viewID);
}
This works, but if I replace the UIView alloc with a UIWebView or UIImageView alloc, then touch event is not triggerred anymore.
How may I make it work ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
设置 userInteractionEnabled=YES 并且如果需要 multipleTouchEnabled=YES
set userInteractionEnabled=YES and if needed multipleTouchEnabled=YES
关于您关于
UIWebView
的问题,我建议您看看drawnonward的答案:UIGestureRecognizer 在 UIWebView 上工作吗?
对于 UIImageView 部分,请参阅此教程:
UIImageView-with-zooming-tapping
我无法将代码粘贴到此处,但我认为这将使该区域保持干净且更易于操作阅读。
Regarding your question about the
UIWebView
I suggest you have a look at drawnonward's Answer :Does UIGestureRecognizer work on a UIWebView?
And for the UIImageView Part see this Tutorial:
UIImageView-with-zooming-tapping
I could not paste the code here but i think this will keep this area cleaner and easier to read.
您必须重写
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
委托函数并返回 YES;
you must override
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
delegate function and return YES;