iOS UITapGestureRecognizer not working sometimes - ios

Sometimes the selector method "tagClickAtIndex" not getting called.
UILabel* label = [[UILabel alloc] init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tagClickAtIndex:)];
[label addGestureRecognizer:tapGesture];
-(void)tagClickAtIndex:(UITapGestureRecognizer*)gesture
{
NSMutableDictionary *mutDict = [[[NSMutableDictionary alloc]initWithDictionary:[_tagArray objectAtIndex:gesture.view.tag]] mutableCopy];
[mutDict setValue:[NSNumber numberWithLong:gesture.view.tag] forKey:#"index"];
[self.delegate tagClickAtIndex:mutDict];
}

you forget to set the frame of label, the reason it only define the clickable area
label.frame = CGRectMake(0,0,200,30)
[yourMainview addSubView:label]
and set if you need
tapGesture.numberOfTapsRequired = 1;

If you are programmatically creating UIlabel,your above code is not working.I tried your code.
UILabel* label = [[UILabel alloc] init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tagClickAtIndex:)];
[label addGestureRecognizer:tapGesture];
It does not show anything.As you have not set the frame and add label view,it does not show.
So Then I tried creating UILabel freshly.I set frame,addSubView to self.view and I set the color as well as number of taps for label.
UILabel* label = [[UILabel alloc] init];
label.frame = CGRectMake(30, 100, 200, 100);
label.text = #"click me";
label.textColor = [UIColor blueColor];
[self.view addSubview:label];
Now Tap gesture recognizer for label
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tagClickAtIndex:)];
tapGesture.numberOfTapsRequired = 1;
label.userInteractionEnabled = YES;
[label addGestureRecognizer:tapGesture];
in delegate method I put log,It calls everytime whenever I touch the label
-(void)tagClickAtIndex:(UITapGestureRecognizer*)gesture
{
NSLog(#"The tap is called");
.........
}
The printed statements shows
The tap is called

Related

UITextField and tap gesture in leftView

I've an icon on leftView of textField and I want to add tap gesture to it. This is my code :
if (!textField.leftView){
UIImageView *infoImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 18)];
infoImage.image = [UIImage imageNamed:#"info"];
infoImage.contentMode = UIViewContentModeCenter;
textField.leftView = infoImage;
textField.leftViewMode = UITextFieldViewModeAlways;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showErrorMessage:)];
tapRecognizer.numberOfTapsRequired = 1;
[infoImage addGestureRecognizer:tapRecognizer];
}
The problem when user click on the image on leftview nothing is happend and the showErrorMessage is not called .
Update
The tap issue was solved by enabling userInteraction on the infoImage.
But now I'm getting [UITapGestureRecognizer tag]: unrecognized selector sent to instance at second line of my Show errorMessage :
-(void)showErrorMessage:(id)sender{
UIImageView *icon = (UIImageView *)sender;
NSString *key = [#(icon.tag) stringValue];
NSLog(#"%#", [_errorDictionary objectForKey:key]);
}
Missing infoImage.userInteractionEnabled = YES;
you have to set the image's property userInteractionEnabled to TRUE

Best way to create a url method in objective c

I have two labels in my app which purely contain URLs:
-(void)openURLA{
NSString *url = #"http://urla.com";
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:url]];
}
-(void)openURLB{
NSString *url = #"http://urlb.com";
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:url]];
}
And this code inside an existing method:
UITapGestureRecognizer *gra = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openURLA)];
UITapGestureRecognizer *grb = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openURLB)];
When a user taps on one of these labels, the openURL methods run fine and the URL opens in safari.
I was wondering how I could create just one method that will open the URL and pass an argument containing label1.text or label2.text values?
I am not 100% sure where to begin doing this so I will appreciate some help.
EDITED:
Follow this whole code:
UILabel * label1 = [[UILabel alloc] initWithFrame:CGRectMake(40, 70, 300, 50)];
label1.backgroundColor = [UIColor redColor];
label1.userInteractionEnabled = YES;
label1.textColor=[UIColor whiteColor];
label1.text = #"http://urla.com";
[self.view addSubview:label1];
UILabel * label2 = [[UILabel alloc] initWithFrame:CGRectMake(40, 130, 300, 50)];
label2.backgroundColor = [UIColor redColor];
label2.userInteractionEnabled = YES;
label2.textColor=[UIColor whiteColor];
label2.text = #"http://urlb.com";
[self.view addSubview:label2];
UITapGestureRecognizer *gsture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openURLS:)];
[label1 addGestureRecognizer:gsture1];
UITapGestureRecognizer *gesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openURLS:)];
[label2 addGestureRecognizer:gesture2];
And call method of UITapGestureRecognizer
- (void)openURLS:(UITapGestureRecognizer*)gesture
{
UILabel *lblUrl=(UILabel *)[gesture view];
NSLog(#"%#", lblUrl.text); // here you get your selected label text.
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:lblUrl.text]];
}
On creating a label set tag like:
label1.tag = 1000;
label2.tag = 1001;
UITapGestureRecognizer *gsture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openURLS:)];
[label1 addGestureRecognizer:gsture1];
UITapGestureRecognizer *gesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openURLS:)];
[label2 addGestureRecognizer:gesture2];
and use the following code to find the view it was tapped
- (void)openURLS:(UITapGestureRecognizer*)sender
{
UIView *view = sender.view;
int tag = view.tag;
if (tag == 1000) {
...
}
}

UILabel on Multi gesture view

I have a third party view from a framework with multiple touch gestures and I want to add a UILabel on top that handles a single tap event. For some reason, the touch event on the UILabel does not work when it is a subview of the third party view, but when I switch the view to a normal UIView it works as intended. Here I make the label:
CGRect frame = CGRectMake(0, 0, 160, 40);
UILabel* label = [[UILabel alloc] initWithFrame:frame];
label.text = [NSString stringWithFormat:#"label"];
label.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(labelTapped:)];
singleTap.numberOfTapsRequired = 1;
[label addGestureRecognizer:singleTap];

(WeeLoader) 'UITapGestureRecognizer' was not declared in this scope

I know that when the error is "'blah' was not declared in this scope" means when the object was not created correctly, but when I am making a Notification Center widget for iOS, using the WeeLoader template and THEOS to compile, I am getting this error: 'UITapGestureRecognizer' was not declared in this scope.
Here is my .mm file:
- (void)loadFullView {
UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
UIImage *bg = [[UIImage imageWithContentsOfFile:#"/System/Library/WeeAppPlugins/WeeAppTest.bundle/WeeAppBackground.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:71];
UIImageView *bgView = [[UIImageView alloc] initWithImage:bg];
bgView.frame = CGRectMake(0, 0, 316, 71);
bgView.userInteractionEnabled = YES;
[bgView addGestureRecognizer:Tap];
[_view addSubview:bgView];
[bgView release];
[Tap release];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 316, 71)];
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.text = #"Hello world";
lbl.textAlignment = UITextAlignmentCenter;
[_view addSubview:lbl];
[lbl release];
}
-(void) handleTapGesture:(UIGestureRecognizer *) sender {
}
What am I doing wrong? I am pretty sure that I am declaring everything correctly, by the way, the code does work with out the UITapGestureRecognizer.
Thank you.
I think there are a few things going on here.
I think you could be getting the out of scope error because the gesture recognizer should be declared in the viewDidLoad or the init methods.
Also, in the UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)]; You call your method handleSingleTap:
Therefore your method should be -(void) handleSingleTap:(UITapGestureRecognizer *) sender

Detect tap on UIImageView inside UIScrollView

I have a horizontal scrollview filled with UIImageViews.
I want to detect a tap on the UIImageView and have its background color changed.
Somehow the tap gesture is not working or so.
However, when I add a tap gesture to the scrollview, it works. The scrollview.background color can be changed.
But I want to detect a tap on the UIImageViews it contains!
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 768, 127)];
[scrollView setScrollEnabled:YES];
scrollView.backgroundColor = [UIColor orangeColor];
[scrollView setShowsHorizontalScrollIndicator:NO];
UIImageView *contentOfScrollView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 1, 1130, 125)];
scrollView.contentSize = CGSizeMake(contentOfScrollView.frame.size.width, contentOfScrollView.frame.size.height);
for (int aantal=0; aantal < 6; aantal++) {
UIImageView *item = [[UIImageView alloc] initWithFrame:CGRectMake(3+(aantal*188), 0, 185, 125)];
item.backgroundColor = [UIColor yellowColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:#selector(imageTapped:)];
tap.numberOfTapsRequired = 1;
tap.cancelsTouchesInView=YES;
item.userInteractionEnabled = YES;
[item addGestureRecognizer:tap];
[contentOfScrollView addSubview:item];
}
//UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
//[scrollView addGestureRecognizer:tap];
scrollView.userInteractionEnabled=YES;
scrollView.delaysContentTouches=NO;
[scrollView addSubview:contentOfScrollView];
[self.view addSubview:scrollView];
And this is the imageTapped function.
-(void)imageTapped:(UITapGestureRecognizer *)gesture
{
NSLog(#"tapped!");
gesture.view.backgroundColor = [UIColor whiteColor];
}
User interaction is set to NO by default for UIImageView, so you need to set it to YES.
You set it to yes for "item", but not for contentOfScrollView.
Your error is here:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:#selector(imageTapped:)];
You need to change the target to "self" instead of "item", then it won't crash.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];

Resources