UITapGesture Selector for UILabel issue - ios

I added tapGesture for self.view and UILabel (subview of mainView), each performs different selectors.
But the only main view tapGesture is being called and label tapgesture is not being called. How it is handled?
Here is the code:
UITapGestureRecognizer *tapGesForSelf = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForSelf:)];
[self.view addGestureRecognizer:tapGesForSelf];
UITapGestureRecognizer *tapLblClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForLbl:)];
[lbl addGestureRecognizer:tapLblClick];
For two selectors only one method is called tapGesForSelf.
Here lbl is the subview of self.view.

Try this
- (void)viewDidLoad {
[super viewDidLoad];
[_lbl setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGesForSelf = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForSelf:)];
[self.view addGestureRecognizer:tapGesForSelf];
UITapGestureRecognizer *tapLblClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForLbl:)];
[_lbl addGestureRecognizer:tapLblClick];
[tapGesForSelf requireGestureRecognizerToFail:tapLblClick];
}
- (void)tapGesForSelf:(UITapGestureRecognizer *)gesture
{
NSLog(#"self");
}
- (void)tapGesForLbl:(UITapGestureRecognizer *)gesture
{
NSLog(#"label");
}

I post answer for your question now once I tried and it worked well.
First see the label in design.I set label text as "Tap Me"
Now I set the code for view and label
- (void)viewDidLoad
{
[super viewDidLoad];
//TapGesture for Label
UITapGestureRecognizer *tapLabel = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(actionTapLabel:)];
tapLabel.delegate = self;
tapLabel.numberOfTapsRequired = 1;
lblTapMe.userInteractionEnabled = YES;
[lblTapMe addGestureRecognizer:tapLabel];
//TapGesture for View
UITapGestureRecognizer *tapMainView = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(actionTapMainView:)];
tapMainView.delegate = self;
tapMainView.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapMainView];
}
//Action method for Label
-(void)actionTapLabel:(UITapGestureRecognizer *)gestureOnLabel{
UILabel *label = (UILabel *)gestureOnLabel.view;
NSLog(#"Lable text is - %#",label.text);
}
//Action method for View
-(void)actionTapMainView:(UITapGestureRecognizer *)gestureOnMainView{
NSLog(#"The Main view is tapped");
}
Output Screenshot

Please setUserInteractionEnabled:YES for interact the TapGesture
[lbl setUserInteractionEnabled:YES];

Related

Button actions and tap gestures are not working on UIView XIB

I added custom UIView with XIB. I just tried to perform button actions and tapgestures , nothing is working eventhough user interaction enabled for all elements including ContentView.
Intializing UIView
-(void)initializeSubviews {
self.backgroundColor = [UIColor clearColor];
[[[NSBundle mainBundle]loadNibNamed:#"DatesView" owner:self options:nil]firstObject];
[self addSubview:self.contentView];
// self.contentView.frame = self.bounds;
[self.fromDateButton addTarget:self action:#selector(tapOnfromDate:) forControlEvents:UIControlEventTouchUpInside];
self.fromDateButton.backgroundColor = [UIColor redColor];
}
Tap gestures
UITapGestureRecognizer *fromDateTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnfromDate)];
[datesView.contentView addGestureRecognizer:fromDateTapRecognizer];
[datesView.fromMonthYearLabel addGestureRecognizer:fromDateTapRecognizer];
[datesView.fromDayLabel addGestureRecognizer:fromDateTapRecognizer];
Button Action
[datesView.fromDateButton addTarget:self action:#selector(tapOnfromDate) forControlEvents:UIControlEventTouchUpInside];
UIView interface
Couple things...
1) By commenting out this line in your view subclass:
// self.contentView.frame = self.bounds;
the view ends up with a frame of 0,0. The button and labels etc are visible because the view defaults to .clipsToBounds = NO, but the objects do not receive user interaction.
2) A UITapGestureRecognizer is a single instance. If you add it to one view, then try to add it to additional views, it will only exist on the last view to which is was added.
Try it like this (be sure to un-comment the above line):
- (void)viewDidLoad {
[super viewDidLoad];
// do all the loading stuff here...
// local declaration
UITapGestureRecognizer *tapRecognizer;
// Optional --- make *sure* user interaction is enabled
[datesView.contentView setUserInteractionEnabled:YES];
[datesView.fromMonthYearLabel setUserInteractionEnabled:YES];
[datesView.fromDayLabel setUserInteractionEnabled:YES];
// new recognizer
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnfromDate:)];
// add it to contentView
[datesView.contentView addGestureRecognizer:tapRecognizer];
// new recognizer
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnfromDate:)];
// add it to fromMonthYearLabel
[datesView.fromMonthYearLabel addGestureRecognizer:tapRecognizer];
// new recognizer
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnfromDate:)];
// add it to fromDayLabel
[datesView.fromDayLabel addGestureRecognizer:tapRecognizer];
// add target action to fromDateButton
[datesView.fromDateButton addTarget:self action:#selector(fromDateButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
- (void) tapOnfromDate: (UITapGestureRecognizer *)recognizer {
NSLog(#"Tapped! %#", recognizer.view);
}
- (void) fromDateButtonTapped: (id)sender {
NSLog(#"Button Tapped! %#", sender);
}

View added manually in code does not appear

Once user double taps on image/screen, it adds an annotation on the screen. When it adds annotation image, I want also open a subview to add annotation detail. However, my subview does not come up.
Here is my code snippet.
#synthesize iViewController;
- (void)viewDidLoad {
[super viewDidLoad];
// adding gesture recognizer
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[self.imvPhoto addGestureRecognizer:tapGesture];
tapGesture.delegate = self;
}
- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateRecognized) {
// Here is the code for subview adding
iViewController = [[InventoryViewController alloc] init];
[self.view addSubview:iViewController.view];
iViewController.view.frame = CGRectMake(100, 100, 320, 460);
}
}
Based on #rdelmar feedback, here is the solution:
iViewController = (InventoryViewController *)[self.storyboardinstantiateViewControllerWithIdentifier:#"InventoryViewController"];
iViewController.view.frame = CGRectMake(728, 32, 300, 736);
[self.view addSubview:iViewController.view];

selector function not called when adding list of image views with tap gesture recognizer

I have trouble with creating list of ImageViews with tap gesture. When I create gesture, selector function is not called. When I create only one imageView function is called. Any idea why? It is all subview of a large scroll view.
This is my code:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTaped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
for(int k=0;k<MyList.count;k++){
for(int i=0;i<listSize;i++){
UIImageView *clickForDetail =[[UIImageView alloc]initWithFrame:CGRectMake(i*HELLO_WIDTH,k*LIST_ROW_HEIGH ,HELLO_WIDTH, LIST_ROW_HEIGH)];
clickForDetail.backgroundColor = [UIColor clearColor];
clickForDetail.tag = tag;
clickForDetail.userInteractionEnabled = YES;
[clickForDetail addGestureRecognizer:singleTap];
[myScroll addSubview:clickForDetail];
tag++;
}
}
and selector function:
-(void)imageTaped: (UITapGestureRecognizer *)recognizer
{
NSLog(#"single Tap on imageview");
}
Is it possible somehow to get tag of a ImageView that is clicked?
You need to put different tap gesture for each view object
for(int k=0;k<MyList.count;k++){
for(int i=0;i<listSize;i++){
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTaped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
UIImageView *clickForDetail =[[UIImageView alloc]initWithFrame:CGRectMake(i*HELLO_WIDTH,k*LIST_ROW_HEIGH ,HELLO_WIDTH, LIST_ROW_HEIGH)];
clickForDetail.backgroundColor = [UIColor clearColor];
clickForDetail.tag = tag;
clickForDetail.userInteractionEnabled = YES;
[clickForDetail addGestureRecognizer:singleTap];
[epgScroll addSubview:clickForDetail];
tag++;
}
}
-(void)imageTaped: (UITapGestureRecognizer *)recognizer
{
NSLog(#"single Tap on imageview");
UIImageView *selectedTextView = (UIImageView *)recognizer.view;
}

How do I get UIGestureRecognizers to work?

I'm having trouble just turning on gesture recognizers in my code. I added a handleTap callback to to the tap gesture recognizer but the print statement never happens. I'm just not having any luck. Does anyone see what I'm doing wrong here?
In my ViewController.h
#interface ViewController : UIViewController<UITextFieldDelegate, UIGestureRecognizerDelegate> {
}
#end
This is what I have in my ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
View *myview = [[View alloc] initWithFrame:applicationFrame];
myview.userInteractionEnabled = YES;
myview.multipleTouchEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleTap)];
tap.numberOfTapsRequired = 1;
tap.delegate = self;
[myview addGestureRecognizer:tap];
self.view = myview;
}
-(void)handleTap:(UITapGestureRecognizer*)recognizer {
NSLog(#"dismiss keyboard");
//[usernameTextField resignFirstResponder];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return YES;
}
Edit: I added a text field and when I click on the text field I see the tap selector print statement. But if I tap off the text field I don't get the selector print statement.
I think the problem is with your selector. Try changing
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleTap)];
And replace it with
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleTap:)];
To make the selector match the signature for handleTap.
You are passing the wrong selector when initialising.
The line should read like this:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleTap:)];
Notice the colon after handleTap to match the method you want to use.
Apparently subclassing the UIView and overriding drawrect with an empty function caused the selector to be called.

Add a gesture recognizer to tableView header doesn't work?

I know that a "table view header"(the most top-part of a table view) is a View
So I try to add a UITapGestureRecognizer to it ,but it doesn't work...
code is simple :
- (void)tap:(UITapGestureRecognizer *)recognizer
{
// do something
}
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
[self.tableView.tableHeaderView addGestureRecognizer:recognizer];
Any tips here to care ? thanks a lot
Here's the thing that works for me:
Instead adding this:
self.tableView.tableHeaderView
I add gesture recognizer on every UILabel on tableview.
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *headerLabel = [[UILabel alloc]init];
headerLabel.tag = section;
headerLabel.userInteractionEnabled = YES;
headerLabel.backgroundColor = [UIColor greenColor];
headerLabel.text = [NSString stringWithFormat:#"Header No.%d",section];
headerLabel.frame = CGRectMake(0, 0, tableView.tableHeaderView.frame.size.width, tableView.tableHeaderView.frame.size.height);
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(catchHeaderGesture:)];
tapGesture.cancelsTouchesInView = NO;
[headerLabel addGestureRecognizer:tapGesture];
return headerLabel;
//return nil;
}
-(void)catchHeaderGesture:(UIGestureRecognizer*)sender
{
UILabel *caughtLabel = (UILabel*)sender.view;
NSLog(#"header no : %d", caughtLabel.tag);
}
I hope that helps.
First of all
Make sure that you call this code section in viewDidLoad or viewWillAppear
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
[self.tableView.tableHeaderView addGestureRecognizer:recognizer];
Second, please make sure that
self.tableView.tableHeaderView
is not null, add
NSLog([self.tableView.tableHeaderView description]);
And check the console for output
I just tried your code and the tap was recieved correctly

Resources