PeterK
PeterK

Reputation: 4301

Getting one, two & three taps working in the same View Controller

I am using UITapGestureRecognizer and is trying to get different taps working on the same ViewController but fail with the one-click tap.

I have tried to find a solution but is not able to integrate it to my test code. I would very much appreciate some help with this so i can learn from it.

here is the code i am using to test on:

The .h file:

@interface ViewController : UIViewController <UIGestureRecognizerDelegate>

The .m file:

@interface ViewController ()

@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer3;
@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer2;
@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer1;

@end

@implementation ViewController

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

return YES;

}

- (void)viewDidLoad
{
[super viewDidLoad];


self.view.backgroundColor = [UIColor greenColor];

// Create Tap Gesture Recognizer

//==1
self.tapGestureRecognizer1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle1Taps:)];
self.tapGestureRecognizer1.numberOfTouchesRequired = 1;
self.tapGestureRecognizer1.numberOfTapsRequired = 1;

//==2
self.tapGestureRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle2Taps:)];
self.tapGestureRecognizer2.numberOfTouchesRequired = 1;
self.tapGestureRecognizer3.numberOfTapsRequired = 2;

//== #3
self.tapGestureRecognizer3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle3Taps:)];
self.tapGestureRecognizer3.numberOfTouchesRequired = 1;
self.tapGestureRecognizer3.numberOfTapsRequired = 3;


[self.tapGestureRecognizer1 requireGestureRecognizerToFail:self.tapGestureRecognizer2];
[self.tapGestureRecognizer1 requireGestureRecognizerToFail:self.tapGestureRecognizer3];
[self.tapGestureRecognizer2 requireGestureRecognizerToFail:self.tapGestureRecognizer3];

// Add gestures
[self.view addGestureRecognizer:self.tapGestureRecognizer1];
[self.view addGestureRecognizer:self.tapGestureRecognizer2];
[self.view addGestureRecognizer:self.tapGestureRecognizer3];

}

- (void)handle1Taps:(UITapGestureRecognizer *)paramSender {
NSLog(@"handle-1-Taps");

NSUInteger touchCounter = 0;

for (touchCounter = 0; touchCounter < paramSender.numberOfTouchesRequired; touchCounter++) {

    CGPoint touchPoint = [paramSender locationOfTouch:touchCounter inView:paramSender.view];

    NSLog(@">>1 x TAP<<");


}

}

- (void)handle2Taps:(UITapGestureRecognizer *)paramSender {
NSLog(@"handle-2-Taps");
NSUInteger touchCounter = 0;

for (touchCounter = 0; touchCounter < paramSender.numberOfTouchesRequired; touchCounter++) {

    CGPoint touchPoint = [paramSender locationOfTouch:touchCounter inView:paramSender.view];

    NSLog(@">>2 x TAP<<");


}

}

- (void)handle3Taps:(UITapGestureRecognizer *)paramSender {
NSLog(@"handle-3-Taps");
NSUInteger touchCounter = 0;

for (touchCounter = 0; touchCounter < paramSender.numberOfTouchesRequired; touchCounter++) {

    CGPoint touchPoint = [paramSender locationOfTouch:touchCounter inView:paramSender.view];

    NSLog(@">>3 x TAP<<");


}

}

@end

The following is the result at the first interaction with the VC:

 - 1 x tap = >>2 x TAP<<
 - 2 x tap = >>2 x TAP<<
 - 3 x tap = >>3 x TAP<<

Upvotes: 0

Views: 1026

Answers (5)

Joe C
Joe C

Reputation: 2834

To do it programmatically (and with some style).

UILongPressGestureRecognizer *longPress;
int idx;

for (idx = 1; idx <= 3; idx++) {
    longPress = [[UILongPressGestureRecognizer alloc]
                                           initWithTarget:self action:@selector(buttonLongPressed:)];
    longPress.minimumPressDuration = min_press_duration;
    longPress.numberOfTapsRequired = 0;
    longPress.numberOfTouchesRequired = idx;
    [button addGestureRecognizer:longPress];
    [longPress release];
}

Upvotes: 0

Schrodingrrr
Schrodingrrr

Reputation: 4271

Add [NSObject cancelPreviousPerformRequestsWithTarget:self]; to handleDoubleTap and handlelTripleTap methods. This will cancel the execution of previous operations.

Upvotes: -1

thatzprem
thatzprem

Reputation: 4767

Looks like there is issue with your logic in below bold_highlighted code lines!

-(void)viewDidLoad { [super viewDidLoad];

self.view.backgroundColor = [UIColor greenColor];

// Create Tap Gesture Recognizer

//==1 self.tapGestureRecognizer1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle1Taps:)]; self.tapGestureRecognizer1.numberOfTouchesRequired = 1; self.tapGestureRecognizer1.numberOfTapsRequired = 1;

//==2 self.tapGestureRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle2Taps:)]; self.tapGestureRecognizer2.numberOfTouchesRequired = 1; self.tapGestureRecognizer3.numberOfTapsRequired = 2;

//== #3 self.tapGestureRecognizer3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle3Taps:)]; self.tapGestureRecognizer3.numberOfTouchesRequired = 1; self.tapGestureRecognizer3.numberOfTapsRequired = 3;

[self.tapGestureRecognizer1 requireGestureRecognizerToFail:self.tapGestureRecognizer2]; [self.tapGestureRecognizer1 requireGestureRecognizerToFail:self.tapGestureRecognizer3]; [self.tapGestureRecognizer2 requireGestureRecognizerToFail:self.tapGestureRecognizer3];

// Add gestures [self.view addGestureRecognizer:self.tapGestureRecognizer1]; [self.view addGestureRecognizer:self.tapGestureRecognizer2]; [self.view addGestureRecognizer:self.tapGestureRecognizer3];

}

Upvotes: 0

Suyog Patil
Suyog Patil

Reputation: 1620

Try this

Comment this statement which is present at three places in your code.

self.tapGestureRecognizer1.numberOfTouchesRequired = 1;

One more issue, replace

self.tapGestureRecognizer3.numberOfTapsRequired = 2;

to

self.tapGestureRecognizer2.numberOfTapsRequired = 2;

as tapGestureRecognizer2 requires number of taps equal to 2

Upvotes: 0

Prashant Nikam
Prashant Nikam

Reputation: 2251

hey Check this : The last line is wrong.

self.tapGestureRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle2Taps:)];
self.tapGestureRecognizer2.numberOfTouchesRequired = 1;
self.tapGestureRecognizer3.numberOfTapsRequired = 2;

it should be

self.tapGestureRecognizer2.numberOfTapsRequired = 2;

Hope this will help you.

Upvotes: 2

Related Questions