I want to create a UIButton that can be held down, when held down it calls the "hold down" action once. when it is released, calls the "hold was release" action.
This code isn't working correctly because the touch can move inside the button and the events aren't triggered in correct order
[button handleControlEvent:UIControlEventTouchDown withBlock:^{
[self performMomentaryAction:PXActionTypeTouchDown];
}];
[button handleControlEvent:UIControlEventTouchUpInside withBlock:^{
[self performMomentaryAction:PXActionTypeTouchUp];
}];
handle control events is based on UIBUtton+block implementation
try this
for NSPratik's case: u can use the event
UIControlEventTouchUpOutside
If user long press button and after some time, instead of releasing the finger, user will move his/her finger out of the bounds of button. just add one more event.Swift Version
You need to connect following two events 1.Touch Down ,2.Touch Up Inside to it's IBAction Methods .It will be fun in Interface Builder. But you can do it programmatically also using addTarget: action: forControlEvents:
You can use Timer and and button touchup to handle this action
this two @IBAction are for handle touch up and touch down button when button touch down timer is started and print something and when button is going u invalidate timer.
access to completed project from Github :
https://github.com/sadeghgoo/RunCodeWhenUIbuttonIsHeld
drag a button from tools(pane) and right click on it.a list will occur find "touch up inside" and click and drag on circle point that on front of text and move it to viewcontroller.h and define a name and in viewcontroller.m do this.
nslog("clicked in");
repeat all action for touch out and find an appropriate event from listI have faced this problem myself, and mostly we use these events:-
// This event works fine and fires
// This does not fire at all
Solution:-
Use Long Press Gesture Recognizer:-
Implementation of gesture:-