[英]How to identify touch on UIView subview
I have a scrollview. 我有一个滚动视图。 Inside the scrollview, I have three subviews, A, B, C. When I click on subview A, I want to get either its tag value or know which view I've clicked.
在滚动视图中,我有三个子视图A,B,C。单击子视图A时,我想获取其标签值或知道我单击了哪个视图。 I've gone through many codes and blogs, but couldn't find a solution for it.
我浏览了许多代码和博客,但找不到解决方案。
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{} //This function is not working with sub view.
Tried these methods but didn't solve my problem. 尝试了这些方法,但没有解决我的问题。
Use UITapGestureRecognizer
. 使用
UITapGestureRecognizer
。 Add a tap gesture recognizer to each of A, B, C views, set the delegate to the view controller and you will be notified on each tap. 向A,B,C视图中的每个视图添加轻击手势识别器,将委托设置为视图控制器,每次轻按都会通知您。
UITapGestureRecognizer* tgrA = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[viewA addGestureRecognizer:tgrA];
...
-(void) handleTapGesture:(UIGestureRecognizer *)sender
{
//sender.view.tag will give you what you need.
}
More information on tap gesture recognizer here: https://developer.apple.com/library/ios/documentation/uikit/reference/UITapGestureRecognizer_Class/Reference/Reference.html 有关点击手势识别器的更多信息,请访问: https : //developer.apple.com/library/ios/documentation/uikit/reference/UITapGestureRecognizer_Class/Reference/Reference.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.