2

I have a view containing two subviews : a bar made of a custom view on top of a UIPageViewController

I want to avoid the user being able to touch the bar and the pageView at the same time (the bar controls the page view and touching them at the same time can cause crashes)

what I did is to set the bar view isExclusiveTouch property to true.

override func awakeFromNib() {
    [...]
    isExclusiveTouch = true
}

The documentation says that if a view isExclusiveTouch property is set to true, other views in the same window should not be able to receive any touch event. Yet I am able to touch and scroll in the page view while touching the bar.

On another screen I have multiple switches made of custom views, I want to avoid multiple switches being touch at the same time so I also put their isExclusiveTouch to true, but still can touch two of them at the same time.

I am missing the isExclusiveTouch is not working at all?

Luinily
  • 66
  • 1
  • 9

2 Answers2

5

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.

Source

Community
  • 1
  • 1
  • Yes, that is the comportment I want. I should not be able to touch the page view while touching the bar but should be able to interact with it while not touching the bar. the problem is that I can interact with the page view while touching the bar. – Luinily Feb 02 '17 at 09:58
  • @Luinily I think you misunderstood, check the answer here he explains how it works : http://stackoverflow.com/questions/843338/why-doesnt-uiview-exclusivetouch-work "It states that the exclusive touch property does NOT affect touches outside the frame of the view." –  Feb 02 '17 at 11:55
  • the documentation example in the link in my says the opposite.. They have two different views (A and B), A having isExclusiveTouch set to true. B is not in the frame of A but under it. "if a user holds one finger inside view A and also touches inside view B, then view B does not receive the touch because view A is the only view tracking touches." I'll still read again your link and try to see if I can get what the documentation says constant with it. – Luinily Feb 03 '17 at 02:33
  • @Luinily I think your UIPageViewController has its own touch events handled inside its own controller. You could manually handle the touch events and make sure to to allow touches on the pageViewController (userInteractionEnabled) to detect when touch state of the "barView" is state==UIGestureRecognizerStateEnded , that is the "simplest" way of doing this I think as a workaround. –  Feb 03 '17 at 07:56
  • Thank you for you help, I could get it work at last, you can check my answer to see how. – Luinily Feb 03 '17 at 10:21
  • @Luinily If you read my answer , where I have bolded it out for you: "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" –  Feb 03 '17 at 10:54
  • The view I added does not covers all the window or everything else but only the view that I set exclusive touch. In my example the Bar has a top view, the page control has not, and I will not be able to interact with the page control while I touch the bar and vice-versa. – Luinily Feb 06 '17 at 01:42
0

I could make this work: I added a transparent view on the front of my bar and set it's isExclusiveTouch to true.

So my bar is now like this

barView -|- subview 1
         |- subview 2
         |- [...]
         |- front view (transparent view for exclusive touch) <- new 

isExclusiveTouch on barView would not work but using it on front view did.

I could also get it to work for my switches with the same solution.

Luinily
  • 66
  • 1
  • 9