我正在使用UITapGestureRecognizer
来检测我的屏幕上点击了哪个UIView但由于某种原因它只检测到父视图点击,例如在代码日志下面只有父视图标记。如何检测主视图中存在的子视图点击。请建议。
Inside View did load :-
UITapGestureRecognizer *viewTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionForViewTapped:)];
[self.view addGestureRecognizer:viewTapRecognizer];
-(void) actionForViewTapped:(UITapGestureRecognizer*)sender {
NSLog(@"view tapped");
UIView *view = sender.view;
NSLog(@"view tag is %lu", view.tag); //Always prints parent view tag.
if(view.tag == 10){
NSLog(@"tag1 tapped"); // not called
}
if(view.tag == 20){
NSLog(@"tag 2 tapped"); // not called
}
}
答案 0 :(得分:3)
您可以使用以下代码检测子视图,我刚刚测试了创建示例项目。
-(void) actionForViewTapped:(UITapGestureRecognizer*)sender {
NSLog(@"view tapped");
UIView *view = sender.view;
CGPoint loc = [sender locationInView:view];
UIView* subview = [view hitTest:loc withEvent:nil];
NSLog(@"view tag is %lu", subview.tag); //will print Subview tag.
if(view.tag == 10){
NSLog(@"tag1 tapped");
}
if(view.tag == 20){
NSLog(@"tag 2 tapped");
}
}
答案 1 :(得分:0)
Provider = Microsoft Software Key Storage Provider
与单个视图一起使用。
如果你想使用UIGestureRecognizer
,你必须为每个视图创建一个,调用相同的方法。