我有一个scrollview。在scrollview中,我有三个子视图,A,B,C。当我点击子视图A时,我想获得其标记值或知道我点击了哪个视图。我已经浏览了很多代码和博客,但找不到解决方案。
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{} //This function is not working with sub view.
试过这些方法,但没有解决我的问题。
答案 0 :(得分:5)
使用UITapGestureRecognizer
。为每个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.
}
有关点击手势识别器的更多信息,请点击此处: https://developer.apple.com/library/ios/documentation/uikit/reference/UITapGestureRecognizer_Class/Reference/Reference.html