On iOS, if a superview's userInteractionEnabled is NO, then all subviews are disabled as well?

前提是你 提交于 2019-11-28 00:38:47

You cant do that,

Instead you would change the arrangment of your views like following:

Main View-> subViews

To

Container View -> Main View that you want to set as inactive
               -> other views that you want to still active

So your current main view and you current subviews will become siblings, children of a new container view

If this may be helpful, I found this in Programming iOS 5 by Matt Neuburg, p. 467:

userInteractionEnabled

If set to NO, this view (along with its subviews) is excluded from receiving touches. Touches on this view or one of its subviews "fall through" to a view behind it.

Further more, Apple's Event Handling Guide for iOS says:

The window object uses hit-testing and the responder chain to find the view to receive the touch event. In hit-testing, a window calls hitTest:withEvent: on the top-most view of the view hierarchy; this method proceeds by recursively calling pointInside:withEvent: on each view in the view hierarchy that returns YES, proceeding down the hierarchy until it finds the subview within whose bounds the touch took place. That view becomes the hit-test view.

and Programming iOS 5 by Matt Neuburg, p.485 mentioned that if a view is marked userInteractionEnabled as NO, or hidden as YES, or opacity is close to 0, then the view and its subview will not be traversed by HitTest (and therefore not considered for any touch).

You can override hitTest(_:withEvent:) to ignore the view itself, but still deliver touches to its subviews.

class ContainerStackView : UIStackView {
    override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        let result = super.hitTest(point, withEvent: event)
        if result == self { return nil }
        return result
    }
}

First Method

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
  if ([touch.view.superview isKindOfClass:[SuperViewParent class]]) return FALSE;
  return TRUE;
}

Second Method

UITapGestureRecognizer *r = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(agentPickerTapped:)];
    r.cancelsTouchesInView = NO;
    [agentPicker addGestureRecognizer:r];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!