UISwitch force drag to change setting - iphone

Is there a way to stop a UISwitch from toggling state when you tap on the inactive side? I would like a control that forces a deliberate 'swipe' action to prevent the user accidentally clicking it. I've had a look around but haven't found any setting that removes the instant toggle on tap.

UISwitch is a child of UIResponder. So you can try to use methods like touchesBegan:withEvent:, touchesMoved:withEvent: and touchesEnded:withEvent: to detect user actions.
The idea is to set some flag to 'changes are not allowed' state, when user began touches. Change it, depending on touch coordinates - for example, change state to 'changes are allowed', if user moved touch more than 25 pixels right. And make real changes when touches ended, depending on final flag's state.

Related

button highlight state and gestures

I've been struggling with this for a while already and some
help would be useful.
Imagine I have a UIButton, which starts in highlighted/selected state.
If a user taps it, then highlighted/selected state changes.
I've implemented this and it works fine. Problems start
for example if user taps inside the button region,
does not release her/his finger, and moves mouse
outside the button area -- at this time my Button
would usually lose highlighted/selected state.
Anyway, I have solved this issue too, by overriding UIControlEventTouchUpOutside
and making button keep the state it had before...
But now another problem comes in, similar to the above,
if a user taps the button, does not release his/her
finger, and moves the finger to the right say (horizontally,
which also makes my dialog for instance go to the right),
then I again lose "selected/highlighted" state....
I believe again some kind of gesture similar to - but different -
than UIControlEventTouchUpOutside is being called which
removes my selected state...
Do you know what can be going in here? Any advice appreciated.
ps. I have fixed all the issues by just setting different images
for normal/selected states using the interface builder.
For the highlighted effect you can use the: setHighlighted of UIButton class.
When you start touching the button set the highlighted property of button to YES.
when you start touch:
yourButton.highlighted = YES;
When you stop touch:
yourButton.highlighted = NO;
I have fixed all the issues by just setting different images for normal/selected states using the interface builder.

How do you disable touch to screen without using beginIgnoringInteractionEvents?

from a similar question/answer
You could always put a transparent UIView over top of the area you
want to "disable" tap input for, have it listen for taps, and have it
ignore them. Remove the UIView (or hide it) when you want input to be
listened to again.
Now, I can understand the strategy, but would someone enlighten me with a code?
How do you make a view
1. listen for taps
2. have it ignore them
Would it not pass touch to views behind it?
Set a plain UIView's userInteractionEnabled property to YES, but don't have any code in there to respond to touch events. The view will then 'swallow' all touches.

Cocoa Touch - Handling touches that get interrupted or lost

I have a couple places in my app where UI elements reset themselves when a touch ends on them. For example, hiding a dashed outline, sliding a view back to the default position.
The problem is that on occasion the app loses track of the touches. One example is if I slide the view upward and cause a UIAlertView to show, the view doesn't slide back, because the reset code is in touches ended. The touch ends during the time the UIAlertView is active, and the view doesn't reset. The same example works for hiding the dashed outlines.
My question is, where/how can I handle the reseting of these custom UI elements so that when a touch ends without being noticed it will still reset. TouchesEnded isn't always doing it for me.
Have you try touchesCancelled? I think it is called when the touch get interrupted.
EDIT: If this doesn't work then maybe you can manually add code that cancel the touch when you initiate the alert.

How to disable multitouch when having several views?

I created my own custom view that extends UIControl. This custom view has its own touch implementation. I implemented touchesBegan, Moved, Ended, and Canceled methods in it.
In the main view controller, I created several instances of this view. So in the screen, there are a number of custom buttons.
I want to disable multitouch in my app. If I click one custom button, then other buttons shouldn't respond.
Actually, it was easy to implement this. While I held some buttons, I could just make other buttons' userInteractionEnabled property to NO until I ended touch.
But the problem is that when I tap these several buttons at the same time, two or more touchesBegan methods work simultaneously, and message passing is messed up.
I tried to set multiTouchEnabled = NO and exclusiveTouch = YES, but it still didn't work.
How can I force to disable multitouch in my app?
Thank you.
You need to set exclusiveTouch to YES, not NO (which is the default anyways). The name of the property refers to the view being the exclusive recipient of any touch events for the duration.

handling same touch event with multiple control (parent and child)

I need to handle a touch event on my custom uiviewcotroller. I have a sub controller within the view that already handle touch event (it's a plot that handle zooming and scrolling).
I want to make a tabbar disappear when I tap the screen once. Actually it only works (even tought the tabbar doesn fade away but simply is no visible) in the areas in which the subcontrol is not present but I need it to work everywhere still handling the subcontrol events.
Make sure you're calling the superclass's event handler method in your event handler method to continue propagation of the event up the responder chain.
Also make sure the subcontrol's exclusiveTouch property is set to NO.
You might want to have a look at the event handling documentation.
Try to set userInteractionEnabled = NO in subcontrol view.
UPD: Try to add transparent button to subcontrol.