Try this:-
Suppose click is the action which gets called on :-
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
What you have to do is:-
Make a BOOL
global variable
in .h file
do:-
BOOL isDoubleClick;
NSTimer *doubleClickTimer;
in .m file do:-
-(IBAction)click
{
if (isDoubleClick==YES) {
//DOUBLE CLICK
isDoubleClick=NO;
}
else if(isDoubleClick==NO)
{
//SINGLE CLICK
isDoubleClick=YES;
doubleClickTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(TimerReload:) userInfo:nil repeats:NO];
}
NSLog(@"bool %s", isDoubleClick ? "true" : "false");
}
-(void)TimerReload:(NSTimer *)timer
{
doubleClickTimer = nil;
isDoubleClick = NO;
}
Explanation:-
First your isDoubleClick
is NO
.When you clicks on the button for the first time it gets YES
.After that again you clicks on the second time if it gets YES
Then it means it is a double click and do whatever you want to perform on double click.Make isDoubleClick
to NO
so that next time click is treated as single click.
Use of Timer:-
For double click you need to ensure that two time click is done one after another.For that enable a timer on single click and after 2 second fire a timer method to set isDoubleClick
to NO
to ensure that now if user clicks on the button , the click will be treated as single not double.
I hope it will save your couple of hours.
Happy Coding!!