UILongPressGestureRecognizer not works with tableview - objective-c

I had added a UILongPressGestureRecognizer to a tableview in my ViewDidLoad method. I added this to detect long press on table view in my code. But it never works. In ViewDidLoad I added this code :
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.resultTableView addGestureRecognizer:lpgr];
[lpgr release];
I also added this method :
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.resultTableView];
NSIndexPath *indexPath = [self.resultTableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
NSLog(#"long press on table view but not on a row");
}
else {
NSLog(#"long press on table view at row %d", indexPath.row);
}
}
Please help me to resolve this?

Your code is working. I think you have to add UIGestureRecognizerDelegate delegate in .h file or how to declare resultTableView i mean you define programmatically or using .xib file.Check it once.
I have tried like this.
resultTableView = [[UITableView alloc] init];
resultTableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420) style:UITableViewStylePlain];
resultTableView.rowHeight = 100.0;
resultTableView.delegate=self;
resultTableView.dataSource=self;
[self.view addSubview:resultTableView];
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[resultTableView addGestureRecognizer:lpgr];
[lpgr release];

It looks like you want to add the gesture to the individual cells, but you are adding the gesture to the table. Try adding the gesture to your UITableViewCell instead.

If the gesture recognizer is being blocked by the UITableView panGestureRecognizer, implement the delegate to ensure both can work
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

Related

How to load new view in the same window on button click?

I've got the View-Controller with a button. I want the new view to be loaded over my View-Controller when the button is pressed. It'd not replace the existing view, I want it to be smaller than the screen and hide when I tap out of the small view.
How should it be implemented in code?
- (IBAction)button:(id)sender
{
UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(0,0,200,200)];
[self.view addSubview:view2];
UITapGestureRecognizer *Tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(Tapview)] autorelease];
[view2 addGestureRecognizer:Tap];
}
-(void)Tapview
{
[view2 removeFromSuperview];
}
Add Tap Gesture to self.view like this:
UITapGestureRecognizer *oneFinger =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(oneFingerAction:)] autorelease];
// Set required taps and number of touches
[oneFinger setNumberOfTapsRequired:1];
[oneFinger setNumberOfTouchesRequired:1];
// Add the gesture to the view
[[self view] addGestureRecognizer:oneFinger];
Add one BOOL flag in .h file; in ViewDidLoad method add this:
flag = FALSE;
Now I assume u have UIView *smallView which be added on screen like this:
[self.view addSubView:smallView];
flag = TRUE;
smallView.center = self.view.centre;
Now when tapped on self.view tap gesture action called
- (void)oneFingerAction:(UITapGestureRecognizer*)sender
{
if(sender.view == self.view)
{
if(flag){
if(smallView)
{
[smallView removeFromSuperView];
}
}
}
}

Tap Recognition for UIImageView in the UITableViewCell

Currently I'm facing a problem, I would like to perform action when the UIImageView on my UITableViewCell had been tapped.
Question: How could I do it? Could any one show me the code, or any tutorial?
Thanks in advance!
This is actually easier than you would think. You just need to make sure that you enable user interaction on the imageView, and you can add a tap gesture to it. This should be done when the cell is instantiated to avoid having multiple tap gestures added to the same image view. For example:
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myTapMethod:)];
[self.imageView addGestureRecognizer:tap];
[self.imageView setUserInteractionEnabled:YES];
}
return self;
}
- (void)myTapMethod:(UITapGestureRecognizer *)tapGesture
{
UIImageView *imageView = (UIImageView *)tapGesture.view;
NSLog(#"%#", imageView);
}
Try this
//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tap];
// handle method
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer
{
RKLogDebug(#"imaged tab");
}
make sure u have....
imageView.userInteractionEnabled = YES;
you can use a customButton instead UIImageView
A tap gesture recogniser can be very memory hungry and can cause other things on the page to break. I would personally reconmend you create a custom table cell, insert a button into the custom cell frame and in the tableview code set the self.customtablecell.background.image to the image you want. This way you can assign the button an IBaction to make it push to whatever view you want.
Use ALActionBlocks to action in block
__weak ALViewController *wSelf = self;
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithBlock:^(UITapGestureRecognizer *weakGR) {
NSLog(#"pan %#", NSStringFromCGPoint([weakGR locationInView:wSelf.view]));
}];
[self.imageView addGestureRecognizer:gr];

Add UIImageView as a subview to UIKeyboard - userInteractionEnabled?

I have a little problem with adding a subview to my UIKeyboard. I can successfully locate my keyboard and add an uiimageview to it, but unfortunately the button under the image view still can be touched. I just wanted to make it a color overlay, which would hide the unneeded keys and also make them untouchable :)
Here is my code, what am I doing wrong? The UIImageView displays correctly, but the buttons still can be tapped... :(
- (UIView *)findKeyboard {
// Locate non-UIWindow.
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
// Locate UIKeyboard.
UIView *foundKeyboard = nil;
for (UIView __strong *possibleKeyboard in [keyboardWindow subviews]) {
// iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView.
if ([[possibleKeyboard description] hasPrefix:#"<UIPeripheralHostView"]) {
possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
}
if ([[possibleKeyboard description] hasPrefix:#"<UIKeyboard"]) {
foundKeyboard = possibleKeyboard;
break;
}
}
return foundKeyboard;
}
- (void)keyboardWillShow:(NSNotification *)note {
UIImageView *overlayView;
UIView *keyboardView = [self findKeyboard];
UIImage *img = [UIImage imageNamed:#"keyboardOverlay.png"];
overlayView = [[UIImageView alloc] initWithImage:img];
[overlayView setUserInteractionEnabled:NO];
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, overlayView.frame.size.width, overlayView.frame.size.height)];
newView.userInteractionEnabled = NO;
newView.multipleTouchEnabled = NO;
[newView addSubview:overlayView];
[keyboardView insertSubview:newView atIndex:40];
}
User interaction being disabled means that touches are passed straight through. Enable it, but don't have any gesture recognisers or actions, and it will swallow all touches.

Delimit UISwipeGestureRecognizer

there is a way to delimit the swipe only on a specific item?
in this way it works everywhere
UIGestureRecognizer *recognizer;
//RIGHT
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight:)];
self.swipeRightRecognizer =(UISwipeGestureRecognizer *)recognizer;
swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRightRecognizer];
self.swipeRightRecognizer = (UISwipeGestureRecognizer *)recognizer;
[recognizer release];
thanks!
You can add the swipe recognizer to only the view you want to swipe. Or, you can do this:
swipeRightRecognizer.delegate = self;
//...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch {
return (touch.view == myViewThatShouldReceiveSwipes);
}

Disable gesture recognizer

I have two types of recognizer, one for tap and one for swipe
UIGestureRecognizer *recognizer;
//TAP
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(numTap1:)];
[(UITapGestureRecognizer *)recognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:recognizer];
self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
recognizer.delegate = self;
[recognizer release];
//SWIPE RIGHT
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight:)];
self.swipeRightRecognizer =(UISwipeGestureRecognizer *)recognizer;
swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRightRecognizer];
self.swipeRightRecognizer = (UISwipeGestureRecognizer *)recognizer;
[recognizer release];
with this function I can disable taps on some objects.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ((touch.view == loseView) || (touch.view == subBgView) || (touch.view == btnAgain)) {
return NO;
}
return YES;
}
How can I disable swipes?
Thanks a lot!
UIGestureRecognizer has a property named enabled. This should be good enough to disable your swipes:
swipeGestureRecognizer.enabled = NO;
Edit: For Swift 5
swipeGestureRecognizer.isEnabled = false
Why don't you set the delegate for the swipe gesture recognizer too and handle them within the same delegate method.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ( [gestureRecognizer isMemberOfClass:[UITapGestureRecognizer class]] ) {
// Return NO for views that don't support Taps
} else if ( [gestureRecognizer isMemberOfClass:[UISwipeGestureRecognizer class]] ) {
// Return NO for views that don't support Swipes
}
return YES;
}
I have a similar issue. Some of my disabled users tap and swipe at the same time, so the app moves to the next screen. I set up an option to allow them to use a three-fingered tap instead. I need to invoke the option the popoverControllerDidDismissPopover delegate and when the app first starts. So I wrote a method that combines the answers above. It looks for all of the swipe gesture recognizers and turns them off, then turns on my tap gesture recognizer.
- (void)changeGestureRecognizer {
// Three finger tap to move to next screen
if ([Globals sharedInstance].useDoubleTapToMoveToNextScreen) {
// Let’s see which gestures are active and turn off the swipes
for (UIGestureRecognizer *gestureRecognizer in self.view.gestureRecognizers) {
NSLog(#"The gestureRecognizer is %#", gestureRecognizer);
if ( [gestureRecognizer isMemberOfClass:[UISwipeGestureRecognizer class]] ) gestureRecognizer.enabled = NO;
}
// Add the three finger tap
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeNext)];
[twoFingerTap setNumberOfTapsRequired:1];
[twoFingerTap setNumberOfTouchesRequired:3];
[self.view addGestureRecognizer:twoFingerTap];
}
}