简体   繁体   English

UIView,它的超级视图和touchesBegan:

[英]UIView, its superview and touchesBegan:

Suppose I have a UIViewController subclass that takes care of some UIViews. 假设我有一个UIViewController子类来处理一些UIViews。 These UIViews are added as subviews of the UIViewController's view property): 这些UIViews被添加为UIViewController的view属性的子视图:

UIViewController: UIViewController中:

- (void)viewDidLoad {
    UIImageView *smallImageView...
    [self.view addSubview:smallImageView];

    UIButton *button..
    [self.view addSubview:button];

    UIView *bigUIView.. // covers the entire screen (frame = (0.0, 0.0, 1024.0, 768.0))
    [self.view addSubview:bigUIView];
...
}

AFAIK, since the bigUIView is the frontmost view and covers the entire screen, it will receive the touchesBegan:withEvent: and the other views, such as button won't receive any touch event. AFAIK,因为bigUIView是最前面的视图并覆盖整个屏幕,它将接收touchesBegan:withEvent:和其他视图,如button不会接收任何触摸事件。

In my application bigUIView must be the topmost view because it holds the main user interface objects (CALayers, actually, the main game objects) that, when animated, must be on top of all other secondary UI elements (UIButtons, etc). 在我的应用程序中, bigUIView必须是最顶层的视图,因为它包含主要的用户界面对象(CALayers,实际上是主要的游戏对象),在动画时,必须位于所有其他辅助UI元素(UIButtons等)之上。 However, I'd like to be able to still have the UIButtons and other objects in the Responder Chain. 但是,我希望能够在响应程序链中保留UIButtons和其他对象。

I tried implementing the following in the bigUIView class: 我尝试在bigUIView类中实现以下内容:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    
    [self.superview touchesBegan:touches withEvent:event];

    ... hit test the touch for this specific uiview..
}

Notes: 笔记:

  1. bigUIView 's superview refers to the UIViewController's view property, does it propagate the touch event to all of it's subviews? bigUIView引用了UIViewController的view属性,它是否将触摸事件传播到它的所有子视图?
  2. bigUIView must have userInteractionEnabled = YES because it also handles user input. bigUIView必须有userInteractionEnabled = YES因为它还处理用户输入。
  3. I can't bring the button / smallImageView to front because it would appear above the main game objects (sublayers of bigUIView ). 我无法将button / smallImageView置于前面,因为它会出现在主游戏对象( bigUIView子层) bigUIView

Thanks in advance. 提前致谢。

Try overriding pointInside:withEvent: in the bigUIView class to return NO if the point is inside the frame rectangle of one of the other subviews of the background view. 尝试重写bigUIView pointInside:withEvent:bigUIView类中,如果该点位于背景视图的其他子视图之一的框架矩形内,则返回NO Since it's a sibling of these other views, the hit testing mechanism will continue testing the other subviews and find the one corresponding to the point the user touched. 由于它是这些其他视图的兄弟,因此命中测试机制将继续测试其他子视图并找到与用户触摸的点相对应的子视图。

Alos, consider implementing the event phase methods ( -touchesBegan:withEvent: , etc.) in your UIViewController subclass rather than in the bigUIView class. Alos,考虑在UIViewController子类而不是bigUIView类中实现事件阶段方法( -touchesBegan:withEvent:等)。 iOS inserts view controllers behind their views in the responder chain, so event messages will bubble up to the view controller unless one of the other subviews (for example, a button) intercepts it. iOS在响应程序链中插入视图后面的视图控制器,因此事件消息将冒泡到视图控制器,除非其他子视图(例如,按钮)中的一个拦截它。 The view controller can then send whatever messages you like to any of its views. 然后,视图控制器可以将您喜欢的任何消息发送到其任何视图。

Normally in the responder chain, events are passes to superviews, not to subviews. 通常在响应者链中,事件传递给超级视图,而不是子视图。 Anyway, I would convert points in events to the button's local coordinate system, and if it hits, send an action to the controller. 无论如何,我会将事件中的点转换为按钮的本地坐标系,如果它命中,则向控制器发送一个动作。

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

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