Why is UIView exclusiveTouch property not blocking

2019-01-19 17:43发布

问题:

I am launching a simple UIView with a textField - let's call it orderSetNameView - upon a button tap. I wish to make this view modal, but without using a

[UIViewController presentModalViewContoller:animated:].

It seems I could simply set textInputView.exclusiveTouch = YES to achieve that.

Apple documentation says about exclusiveTouch:

A Boolean value indicating whether the receiver handles touch events exclusively. If YES, the receiver blocks other views in the same window from receiving touch events; otherwise, it does not. The default value is NO.

I assume "same window" means same UIWindow, of which I have only one.

The problem is that when I instantiate my orderSetNameView, add it as a subview, and set exclusiveTouch = YES, touch events happen in all other views of my app, i.e., touch events in other views are not blocked as expected.

    // ....

    [self.view addSubview:self.orderSetNameView];
    [self.orderSetNameView openWithAnimationForAnimationStyle:kMK_AnimationStyleScaleFromCenter];
}

// Set as modal
self.orderSetNameView.exclusiveTouch = YES;

Shouldn't orderSetNameView block touch events in all other views? What am I missing?

回答1:

From Apple developer forums:

exclusiveTouch only prevents touches in other views during the time in which there's an active touch in the exclusive touch view. That is, if you put a finger down in an exclusive touch view touches won't start in other views until you lift the first finger. It does not prevent touches from starting in other views if there are currently no touches in the exclusiveTouch view.

To truly make this view the only thing on screen that can receive touches you'd need to either add another view over top of everything else to catch the rest of the touches, or subclass a view somewhere in your hierarchy (or your UIWindow itself) and override hitTest:withEvent: to always return your text view when it's visible, or to return nil for touches not in your text view.