ios - 如何识别 UIView subview 上的触摸

标签 ios objective-c uiview

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

相关文章:

objective-c - 对Objective-C中具有不同键值对象的两个数组进行排序和合并再排序

iphone - UITextField 文本更改回调

ios - UIView 和 CALayer 之间有什么区别?

ios - 仅在 Apple Store 为员工部署应用程序

ios - 如何使用 Frank ios 传递带有撇号的辅助功能标签?

ios - UIPopOverControl 和 UIImageView

objective-c - 使用 UIScrollView subview 调整 UIView 的大小

ios - 将 UILabel 设置为隐藏

ios - EXC_BAD_ACCESS(SIGSEGV)和AVAudioSession

ios - SpriteKit 物理不工作