我有一个 ScrollView 。在 ScrollView 中,我有三个 subview ,A、B、C。当我单击 subview A 时,我想获取它的标记值或知道我单击了哪个 View 。我浏览了许多代码和博客,但找不到解决方案。
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{} //This function is not working with sub view.
尝试了这些方法,但没有解决我的问题。
最佳答案
使用UITapGestureRecognizer
.为 A、B、C View 中的每一个添加一个点击手势识别器,将委托(delegate)设置为 View Controller ,每次点击都会通知您。
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.
}
有关点击手势识别器的更多信息,请点击此处:
https://developer.apple.com/library/ios/documentation/uikit/reference/UITapGestureRecognizer_Class/Reference/Reference.html
关于ios - 如何识别 UIView subview 上的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19440499/