ios - UIButton with hold down action and release action -
i want create uibutton can held down, when held down calls "hold down" action once. when released, calls "hold release" action.
this code isn't working correctly because touch can move inside button , events aren't triggered in correct order
[button handlecontrolevent:uicontroleventtouchdown withblock:^{ [self performmomentaryaction:pxactiontypetouchdown]; }]; [button handlecontrolevent:uicontroleventtouchupinside withblock:^{ [self performmomentaryaction:pxactiontypetouchup]; }];
handle control events based on uibutton+block implementation
try this
uibutton *abutton = [uibutton buttonwithtype:uibuttontyperoundedrect]; abutton.frame = cgrectmake(xvalue, yvalue, 45, 45); [abutton addtarget:self action:@selector(holddown) forcontrolevents:uicontroleventtouchdown]; [abutton addtarget:self action:@selector(holdrelease) forcontrolevents:uicontroleventtouchupinside]; - (void)holddown { nslog(@"hold down"); } - (void)holdrelease { nslog(@"hold release"); }
for nspratik's case: u can use event uicontroleventtouchupoutside
if user long press button , after time, instead of releasing finger, user move his/her finger out of bounds of button. add 1 more event.
uibutton *abutton = [uibutton buttonwithtype:uibuttontyperoundedrect]; abutton.frame = cgrectmake(xvalue, yvalue, 45, 45); [abutton addtarget:self action:@selector(holddown) forcontrolevents:uicontroleventtouchdown]; [abutton addtarget:self action:@selector(holdrelease) forcontrolevents:uicontroleventtouchupinside]; [abutton addtarget:self action:@selector(holdreleaseoutside) forcontrolevents:uicontroleventtouchupoutside]; //add case releasing finger out side of button's frame //add method along other methods - (void)holdreleaseoutside { nslog(@"hold release out side"); }
swift version
var abutton:uibutton = uibutton.buttonwithtype(uibuttontype.custom) uibutton abutton.frame = cgrectmake(xvalue,yvalue, 45, 45) abutton.settitle("abutton", forstate: uicontrolstate.normal) abutton.backgroundcolor = uicolor.greencolor() abutton.addtarget(self, action: selector("holdrelease:"), forcontrolevents: uicontrolevents.touchupinside); abutton.addtarget(self, action: selector("holddown:"), forcontrolevents: uicontrolevents.touchdown) self.addsubview(testbutton) //target functions func holddown(sender:uibutton) { println("hold down") } func holdrelease(sender:uibutton) { println("hold release") }
Comments
Post a Comment