1

我有一个滚动视图。在滚动视图中,我有三个子视图,A、B、C。当我单击子视图 A 时,我想获取它的标记值或知道我单击了哪个视图。我浏览了许多代码和博客,但找不到解决方案。

    - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {}
    - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{} //This function is not working with sub view.

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

4

1 回答 1

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

于 2013-10-18T02:23:09.673 回答