2

I have a uiswitch in settings that allows users to view some sensitive things in the app. Before allowing users to change the switch through a tap I want to prompt them to provide a password.

I tried disabling the switch but it's impossible (without some kludgy workarounds) to detect a tap/touch from a disabled switch.

Is there an event or way to detect the touch/tap before the uiswitch changes value so that I can prevent it from changing value and show the prompt?

I tried using touchdown and touch inside but they don't stop the change in value.

Thanks for any suggestions.

AVerguno
  • 1,277
  • 11
  • 27
Arjun
  • 89
  • 1
  • 9
  • 3
    React to the switch normally. Reset the switch if the user doesn't enter the correct password. – rmaddy Jan 30 '17 at 19:50

1 Answers1

11

Here's an idea. Disable the switch as you said already. But also have another view behind the switch, a view which is the same size as the switch and has a UITapGestureRecognizer behind it.

Do you see what will happen? If the switch is disabled, the touch will fall thru to the view behind it, and the tap gesture recognizer will fire. Thus you will know that the user tried to tap the switch.

But once the password has been given and the switch is enabled, you can ignore the tap gesture recognizer (in fact it will probably never fire again, because the switch, being in front of it, will eat the touch).

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thinking out side of the box, sometimes that s all that it takes :) – TheFuquan Jan 30 '17 at 21:14
  • Disabling the switch (isEnabled = false) works well but it changes how the switch displays in the UI - a little faded. Using "isUserInteractionEnabled = false" not only allows the touch to fall through to the view behind but doesn't affect the UI. – MacMc Jul 09 '23 at 06:42