Re: How to know when a UISwitch is being touched
Re: How to know when a UISwitch is being touched
- Subject: Re: How to know when a UISwitch is being touched
- From: Brian Slick <email@hidden>
- Date: Wed, 01 Apr 2009 20:22:39 -0400
For my case, I just needed to know when the value changed. So I
adapted what you came up with to look like this:
[switchView addTarget:self action:@selector(switchTouched:)
forControlEvents:UIControlEventValueChanged];
UIControlEventAllTouchEvents was sending 3 messages to switchTouched:
each time the switch was touched - I'm guessing touch down, touch up,
value changed - but the state wasn't correct until the second or
sometimes third message. So I tried ValueChanged, and only got a
single message with the correct state. Right now I only have a single
switch that I'm handling this way, but when I add more, I can sort
them out by tag to respond accordingly in switchTouched.
Thanks for the help! I'm new to target/action, but it sure is handy.
Brian
On Apr 1, 2009, at 3:56 PM, Joan Lluch-Zorrilla wrote:
Using isOn gives you whether the switch is on of off. I'm not sure
if this is what you are looking for. In my case I wanted to know
whether the user is manipulating (touching) the switch, regardless
of its state. I found a way to do it. The code is as follows:
Add this upon creation of the switch.
[theSwitch addTarget:self action:@selector(controlTouched:forEvent:)
forControlEvents:UIControlEventAllTouchEvents] ;
Add the following to the class referred by self in the above sentence.
- (void)controlTouched:(UIControl *)sender forEvent:(UIEvent*)event
{
UITouch *touch = [[event allTouches] anyObject] ;
BOOL tag ;
if ( touch == nil ) tag = NO ;
else
{
UITouchPhase phase = [touch phase] ;
tag = (phase != UITouchPhaseEnded && phase !=
UITouchPhaseCancelled) ;
}
[sender setTag:tag] ;
}
Now you have 1 in the tag property if the switch is being touched,
or 0 otherwise.
Hope that helps.
Joan Lluch-Zorrilla
El 01/04/2009, a las 16:03, Brian Slick escribió:
We may be able to combine forces here. What I was doing gets the
correct answer from the switch, but what I was finding is that
certain circumstances do not result in a message being sent from
the switch. I forget just now which circumstance it was, but I
believe it was a direct tap on the "button" of the switch. Swiping
the switch worked fine, tapping on the text of the switch worked
fine, but tapping on the button did not send a message. I finally
just punted and asked each switch for their current status in
viewWillDisappear, but this means I cannot react to changes while
the user is still in the view.
So, if you use your method, but change it to [sender isOn], does
that work? I hope so, because that opens up some possibilities for
me.
Brian
On Mar 31, 2009, at 9:22 AM, Joan Lluch-Zorrilla wrote:
I need to know whether the user is touching inside a UISwitch
control. The UIControlEvent(s) do fire, but then isTouchInside
always gives NO. My code is:
- (void)switchTouched:(UIControl *)sender
{
NSLog( @"switchtouched:%d", [sender isTouchInside]) ;
}
The switchTouched method was added as a target to touch events
upon creation of the switch like this
[switchv addTarget:self action:@selector(switchTouched:)
forControlEvents:UIControlEventAllTouchEvents] ;
Whatever the user does on the switch the result is always Zero.
What am I missing?
Joan Lluch-Zorrilla
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden