简体   繁体   English

处理多个子视图中的触摸事件

[英]Handling a touch event across multiple subviews

I am new to iOS development. 我是iOS开发的新手。 I have a custom drawn view which is composed of multiple subviews covering the target area on screen. 我有一个自定义绘制视图,它由覆盖屏幕上目标区域的多个子视图组成。 Specifically this is a board game like chess where I used a view for each square. 具体来说,这是一个像国际象棋这样的棋盘游戏,我使用了每个方块的视图。 The squares are created as subviews on a UIView. 方块在UIView上创建为子视图。 There is one UIViewController for this. 这有一个UIViewController。 From what I read, I have to have touchesBegan, touchesEnded etc calls in my UIView to handle these. 从我读到的内容来看,我必须在我的UIView中使用touchesBegan,touchesEnded等来处理这些问题。 But none of these functions are getting called. 但这些功能都没有被调用。 I added these calls on the base view and all the subviews. 我在基本视图和所有子视图上添加了这些调用。 So - 所以 -

  1. How do I simulate these touch events in the iOS simulator? 如何在iOS模拟器中模拟这些触摸事件? A mouse click is not calling the touchesBegan ,touchesEnded calls on any view. 鼠标单击不会调用任何视图上的touchesBegan,touchesEnded调用。

  2. Ideally I would like to handle these in the UIViewController because I want to run the touch through some logic. 理想情况下,我想在UIViewController中处理这些因为我想通过一些逻辑运行触摸。 Is that possible? 那可能吗? How do I achieve it? 我该如何实现?

Please refer THIS It is tutorial in Apple sample codes it describes how to handle touches very nicely. 请参考是Apple示例代码中的教程,它描述了如何非常好地处理触摸。 Just run the sample code and go through description you will get clear idea how touches work un iOS. 只需运行示例代码并通过描述,您将清楚地了解触摸如何在iOS上工作。

Turns out when I add the view programmatically and not through the storyboard the userInteractionEnabled variable is set to NO by default. 当我以编程方式添加视图而不是通过故事板时,默认情况下userInteractionEnabled变量设置为NO。 After setting it up, I get the touchesEnabled call getting called in the view. 设置完成后,我在视图中调用了touchesEnabled调用。

Check this : 检查一下:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if(![touch.view isKindOfClass:[yourView class]])
    {
    }
}

Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM