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];
}
Related
I have a button that behaves in one way when user taps on it and another when user double taps on it. In case the user double taps the button, i don't want the single tap behavior to happen.
How can i prevent the call to the touch down event in case double tap was recognized?
You can use Target-action; For UIControlEvents, you can use "UIControlEventTouchDown" and "UIControlEventTouchDownRepeat" like this:
UIButton * button = [UIButton buttonWithType:UIButtonTypeContactAdd];
button.frame = CGRectMake(150, 200, 50, 50);
[button addTarget:self action:#selector(buttonSingleTap:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:#selector(buttonMutipleTap:) forControlEvents:UIControlEventTouchDownRepeat];
[self.view addSubview:button];
- (void)buttonSingleTap:(UIButton *)btn{
[self performSelector:#selector(buttonAction:) withObject:btn afterDelay:0.5];
}
- (void)buttonAction:(UIButton *)sender{
NSLog(#"single tap");
}
- (void)buttonMutipleTap:(UIButton *)btn{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(buttonAction:) object:btn];
NSLog(#"mutiple tap!");
}
But there will be 0.5sec to delay!
As I understand you want different behavior on the same button,So simply apply two different tap gesture.The below code may help you.
UIButton *btn1=[[UIButton alloc]init]; //your button
//Two diff method call for two diff behaviour
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapEvent:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTapEvent:)];
//specify the number of tapping event require to execute code.
singleTap.numberOfTapsRequired = 1;
doubleTap.numberOfTapsRequired = 2;
[singleTap requireGestureRecognizerToFail:DoubleTap];
//Apply multiple tap gesture to your button
[btn1 addGestureRecognizer:singleTap];
[btn1 addGestureRecognizer:doubleTap];
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;
}
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];
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];
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:)];