我有一个像这样生成的视图:
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 alloc替换UIView alloc,则不再触发触摸事件。
我怎样才能让它发挥作用?
答案 0 :(得分:0)
设置userInteractionEnabled = YES,如果需要,设置multipleTouchEnabled = YES
答案 1 :(得分:0)
关于UIWebView
的问题,我建议您查看 drawnonward's 答案:
Does UIGestureRecognizer work on a UIWebView?
对于UIImageView Part,请参阅本教程:
UIImageView-with-zooming-tapping
我无法将代码粘贴到此处,但我认为这样可以使这个区域更清晰,更易于阅读。
答案 2 :(得分:0)
你必须覆盖
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
委托功能并返回YES;