How to trigger UIControlEventTouchCancel? - iphone

I thought UIControlEventTouchCancel is triggered when I tap on a button and then drag out of it. But I'm doing exactly that and my event handler is never called.
I have a class that inherits from UIControl and I'm doing this in the init:
[self addTarget:self action:#selector(onTouchCancel)forControlEvents:UIControlEventTouchCancel];
How do I trigger the cancel event?
Thanks for your help!

Use "UIControlEventTouchUpOutside" if you want an event when the finger is outside the bounds of the control.

Related

Button down/up in objective c for iphone?

I have a button, and I'd like to call a function when the user pushes it then call a different function when he releases it.
Right now I'm using this to create the button:
[ScanButton addTarget:self action:#selector(scanButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
to call scanButtonPressed. As of now scanButtonPressed is only called upon release. How do I change this?
Thanks
How about by trying a control event other than UIControlEventTouchUpInside?
Try - UIControlEventTouchDown .
This contains all of the different events you can pass to a button
UIControl Class Reference. Try looking at these; both UIControlEventTouchUpInside and UIControlEventTouchDown will work.

UISlider problem - how to detect a user releasing the slider button in iOS?

I can detect the value change event from the UISlider, but now I need to detect a user releasing the slider button.
How to detect this event (or touch on slider ends) ?
Should I use the "touch up inside" event for detection ?
Thanks.
You can also use the UISlider's UIControlEventValueChanged. Set the continuous property to NO as to initiate when the user is done selecting a value.
[mySlider addTarget:self action:#selector(myMethod) forControlEvents:UIControlEventValueChanged];
mySlider.continuous = NO;
I've not worked with sliders, however as UISlider inherits from UIView - surely you can just tap into touchesBegan: and touchesEnded: to perform your task.
Through IB you can attach "Touch Up Inside" and "Touch Up Outside" to a method/action of the viewController for slider release.
Or more programmatically (probably in your viewController), you can use
- (void)addTarget:(id)target action:(SEL)action
forControlEvents:(UIControlEvents)controlEvents
to detect when the UIControlEventTouchUpOutside and UIControlEventTouchUpInside events occur to your UISlider.
References
method
control events
UIControlEventTouchDown event to detect when the user starts using the control. UIControlEventTouchUpInside when the user finishes using the slider. To add the event just do as #JohnK suggested

Button should be visible when the user is done selecting the value through slider

I need to implement such kind of functionality in my slider such that the submit button should not appear till the user pull up his fingers from slider.
Is there any kind of function which can duplicate this.
Thanks,
The UISlider has a continuous property. Set this to NO, and you will only receive valueChanged events when the user is done choosing his value. Then add a target for the valueChanged event, and there you can set the button to be enabled.
If you can't set continuous to NO for some reason, observe the UIControlEventTouchUpOutside and UIControlEventTouchUpInside events:
[yourSlider addTarget:self
action:#selector(showSubmit:)
forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
And
- (void)showSubmit:(UISlider *) {
// code to show your button here
}

UiTextField events

When I create UITextField inside Interface Builder, I can access Events tab for it, which has events like Value changed, Touch cancel, Touch drag, etc. I can assign my own methods to every of those events. How can I do the same, when I create UITextField programmatically with alloc?
Refer to Apple documentation for UIControl. After initializing your textField, call addTarget:action:forControlEvents:
example for the touch event ending an edit session
[textField addTarget:self action:#selector(handleTouchValueChanged:) forControlEvents: UIControlEventEditingDidEnd]
Instead of UIControlEventValueChanged, you should use UIControlEventEditingChanged:
[_titleTextField addTarget:self action:#selector(handleTitleValueChanged:) forControlEvents:UIControlEventEditingChanged];
UIControlEventEditingChanged fires whenever user changes value [synchronous with typing or keyup]
which could cause extra hits to your data handler routine, if you're saving values based on that event, expecting it to be some kind of final value from user input...

How can I determine whether my UIButton's event is Touch Down?

How can I determine whether my button's event is Touch Down?
I want to do a function like this:
if(users click on touchdown event)
{
NSLog(#"a");
}
else if(users click on touchupinside event)
{
NSLog(#"b");
}
Eather you set two different IBAction methods in the InterfaceBuilder or you set two different targets via:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
in your code while creating the button.
You "find out" by letting the button tell you when the event happens.
Add a method (or methods) like this:
- (IBAction)myButtonClick:(id)sender;
In Interface Builder, attach the method(s) to the events you're interested in.
You create a separate method for each type of event if you want different behavior for a TouchDown as opposed to TouchUpInside.
you attach each unique event to its own IBAction