UILongPressGestureRecognizer sometimes take to fail recognizer of other gestures - ios

My UILongPressGestureRecognizer sometimes take to fail recognizer of other gestures.
I cannot understand the exact pattern that break the recognizer of the 2 single tap gesture. I think it will happen if the user click a lot of time on my UIImageView.
I think that is related to the logic of my code... but I can't understand where is the fail.
Thanks
// adding tap gesture recognizers to the image view
UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap)] autorelease];
UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap)] autorelease];
UILongPressGestureRecognizer *longTap = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handlelongTap)] autorelease];
[singleTap setNumberOfTapsRequired:1];
[doubleTap setNumberOfTapsRequired:2];
longTap.minimumPressDuration=0.70;
longTap.numberOfTouchesRequired=1;
[singleTap requireGestureRecognizerToFail:doubleTap];
[singleTap requireGestureRecognizerToFail:longTap];
[doubleTap requireGestureRecognizerToFail:longTap];
[longTap requireGestureRecognizerToFail:singleTap];
// adding drag and drop gesture recognizers to the image view
UIGestureRecognizer *panGesture = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(pan:)] autorelease];
//Adding gestures to the imageView
[imageView addGestureRecognizer:singleTap];
[imageView addGestureRecognizer:doubleTap];
[imageView addGestureRecognizer:longTap];
[imageView addGestureRecognizer:panGesture];

Related

Ios- Ipad- UicollectionViewCell- icon on a cell- how to add tap gesture to icon in xcode 5?

I have used a collection view cell to display multiple images. Then each image will have a small icon on top right. My question is how to add a tap gesture to a small icon that's on top right of each cell image.
My code:
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(projectSetting:)];
_projectSettingIconButton = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"login-settings.png"]];
_projectSettingIconButton.frame = CGRectMake(95, 3, 21, 21);
[_projectSettingIconButton addGestureRecognizer:tapped];
[cell addSubview:_projectSettingIconButton];
Try This instead
Add tap Gesture Recogniser on the icon :
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[YOURICON addGestureRecognizer:singleTap];
Then Add
-(void)handleSingleTap:(id)sender
{
// do your stuff;
}

pan gesture and swipe gesture

In my project I need a swipe gesture action for left right movement and I need a pan gesture action for top bottom directions. When I put both gestures, only pan gesture is invoking. Does anyone have a solution for this?
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(goToPreviosSong)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:rightSwipe];
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(goToNextSong)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:leftSwipe];
for pan i'm using this code
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panAction:)];
[self.view addGestureRecognizer:edgePanGesture];
[panGesture requireGestureRecognizerToFail:rightSwipe];
[panGesture requireGestureRecognizerToFail:leftSwipe];
Use requireGestureRecognizerToFail: to recognize the gesture if the other gesture recognizer did not executes.
[rightSwipe requireGestureRecognizerToFail: panGesture];
[leftSwipe requireGestureRecognizerToFail: panGesture];

How to fire UILongPressGestureRecognizer quickly

I want to hover a cell as soon as it is being pressed, but the press takes too long. I tried setting the minimum press time to .05, but it doesn't affect the recognizer.
Any suggestions:
if (cell.gestureRecognizers.count==0) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(userCellTapped:)];
[tap setNumberOfTapsRequired:1];
//add a long press that kills the tap if recognized
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(userCellPressed:)];
[press setMinimumPressDuration:.05];
[press setNumberOfTouchesRequired:1];
[press requireGestureRecognizerToFail:tap];
[tap requireGestureRecognizerToFail:press];
[cell addGestureRecognizer:tap];
[cell addGestureRecognizer:press];
}

UINavigationItem touchView touch size

I'm setting a view on my UINavigationItem's touchView and adding a tap gesture recognizer to it. What's weird, is that the tap recognizer is getting called even when the tap is outside of the view. Any idea why this might be happening?
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 120.0f, 20.0f)];
testView.backgroundColor = [UIColor redColor];
UIGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doStuff:)];
tapRecognizer.cancelsTouchesInView = YES;
[testView addGestureRecognizer:tapRecognizer];
testView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.navigationItem.titleView = testView;
I can click outside the red box and still trigger the gesture recognizer.
use this way
UITapGestureRecognizer *tapRecognizer =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(doStuff:)];
instead of this code
UIGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doStuff:)];

UIGestureRecognizers interfering with UIToolbar?

I'm working on an OpenSource Magazine Engine that you can look at on GitHub:
https://github.com/interactivenyc/Defrag
I've set up a UIToolbar in a UIView I've called MenuPanel. For some reason the UIBarButtonItems in the UIToolbar aren't calling their actions properly. Here is the syntax I'm using for the buttons:
UIBarButtonItem *homeItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"home.png"] style:UIBarButtonItemStylePlain target:self action:#selector(buttonClicked:)];
What is happening is that anywhere I click on my screen, the UITapGestureRecognizer declared in my main UIViewController is being called instead. This gets setup in this block of code in my main UIViewController:
- (void)setupGestureRecognizers {
//NSLog(#"setupGestureRecognizer NEW");
UISwipeGestureRecognizer *swipeRecognizer;
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
UITapGestureRecognizer *tapRecognizer;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[self.view addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
}
I'm sure I have something pretty basic conceptually wrong with how I'm trying to do this. Can someone tell me how I can fix this problem?
For reference, you can see my main DefragViewController: UIViewController here:
https://gist.github.com/1431722
And my MenuPanel: UIView here:
gist.github.com/1431728
I solved my own question.
I had to tell my UIViewController to ignore touches from any UIToolbar like this:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//ignore any touches from a UIToolbar
if ([touch.view.superview isKindOfClass:[UIToolbar class]]) {
return NO;
}
return YES;
}

Resources