如何识别UIView子视图上的触摸

时间:2013-10-18 02:14:45

标签: ios objective-c uiview

我有一个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.

试过这些方法,但没有解决我的问题。

1 个答案:

答案 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