tapGesture is not working for labels - ios5

UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(openNewView:)];
UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(openNewView:)];
[writeReview_view setTag:1];
[map_view setTag:2];
[writeReview_view addGestureRecognizer:tapGesture1];
[map_view addGestureRecognizer:tapGesture2];
-(void)openNewView:(UITapGestureRecognizer *)recog1
{
NSLog( #"recog1.view.tag == %d",recog1.view.tag);
if (recog1.view.tag==2)
{
[self performSegueWithIdentifier:#"mapsegue" sender:self];
}
else
{
[self performSegueWithIdentifier:#"loginsegue" sender:self];
}
}

Enable userInteractionEnabled on the label
label.userInteractionEnabled = YES;

Related

How to zoom into a selected area of image in iPhone?

I have an ImageView when clicked on that imageview, transparent circle should be created and again when double clicking on that circle, particular Image in that circle area should be zoomed.Any suggestion would be appreciated.
I think this code will helpfull to you
- (id)initWithImage:(UIImage *)image {
self = [super initWithImage:image];
if (self) {
[self setUserInteractionEnabled:YES];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTwoFingerTap:)];
[doubleTap setNumberOfTapsRequired:2];
[twoFingerTap setNumberOfTouchesRequired:2];
[self addGestureRecognizer:singleTap];
[self addGestureRecognizer:doubleTap];
[self addGestureRecognizer:twoFingerTap];
[singleTap release];
[doubleTap release];
[twoFingerTap release];
}
return self;
}
#pragma mark Private
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
if ([delegate respondsToSelector:#selector(tapDetectingImageView:gotSingleTapAtPoint:)])
[delegate tapDetectingImageView:self gotSingleTapAtPoint:[gestureRecognizer locationInView:self]];
}
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
if ([delegate respondsToSelector:#selector(tapDetectingImageView:gotDoubleTapAtPoint:)])
[delegate tapDetectingImageView:self gotDoubleTapAtPoint:[gestureRecognizer locationInView:self]];
}
- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer {
if ([delegate respondsToSelector:#selector(tapDetectingImageView:gotTwoFingerTapAtPoint:)])
[delegate tapDetectingImageView:self gotTwoFingerTapAtPoint:[gestureRecognizer locationInView:self]];
}

UITapGestureRecognizer for UIImageView on UIScrollView never called iOS5

I am adding a UIImageView as a subview to a UIScrollView then i set the image.
Im trying to to use UITapGestureRecognizer.
The selector of the UITapGestureRecognizer is never called in iOS 5 (in iOS 6 it DOES!).
Tried many variations. this is my code:
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(index*(IMAGE_WIDTH+10), 10.0, IMAGE_WIDTH, IMAGE_HEIGHT)];
[imgView setImageWithURL:[NSURL URLWithString:meal.RecommendedImageURL] placeholderImage:[UIImage imageNamed:#""]];
imgView.layer.cornerRadius = 4;
[imgView.layer setMasksToBounds:YES];
[imgView setUserInteractionEnabled:YES];
[imgView setMultipleTouchEnabled:YES];
[scrollView addSubview:imgView];
if (IOS_NEWER_OR_EQUAL_TO_5)
{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[imgView addGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.enabled = YES;
tapGestureRecognizer.delegate = self;
[tapGestureRecognizer setCancelsTouchesInView:NO];
}
this is my selector which is only called in iOS5:
- (void) imageTapped: (UITapGestureRecognizer *)recognizer
{
//Code to handle the gesture
UIImageView *tappedImageView = (UIImageView*)recognizer.view;
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView = tappedImageView;
vc.liftedImageView.contentMode = UIViewContentModeScaleAspectFit;
if (IOS_NEWER_OR_EQUAL_TO_5) {
[self.parentViewController presentViewController:vc animated:YES completion:nil];
}
else
{
[self.parentViewController presentModalViewController:vc animated:YES];
}
}
In addition to that, i tried to setCancelsTouchesInView to YES in my UIScrollView but it doesn't work either.
Thanks for your help!
try to add Gesture in Scrollview then check iskind of class image view
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[scrollview addGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.enabled = YES;
tapGestureRecognizer.delegate = self;
[tapGestureRecognizer setCancelsTouchesInView:NO];
- (BOOL)gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// test if our control subview is on-screen
if ([touch.view isKindOfClass:[UIImageView class]]) {
// we touched a button, slider, or other UIControl
return YES;
}return NO;
}
Implement the 3 methods of UIGestureRecognizerDelegate & return default values:
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
Ok i solved it very EASILY!
the problem was setting the delegate of the UITapGestureRecognizer to self.
when i removed the delegate = self, it started working :)
Thanks for your assistance

Recognizing multiple UILabels tap for UITapGestureRecogniser

In my view load I have two UILabels and I have added same tapGesture for both.If a particular label is tapped then its functionality should be performed.But I m unable to do so ?
-(void)viewDidLoad{
lblEditProfile.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelClicked:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[lblEditProfile addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
lblViewDetails.userInteractionEnabled = YES;
tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelClicked:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[lblViewDetails addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
}
-(IBAction)labelClicked:(UITapGestureRecognizer*)tapGestureRecognizer
{
currentLabel = (UILabel *)tapGestureRecognizer.view;
NSLog(#"tap %#",tapGestureRecognizer.view);
if(currentLabel.text==#"Edit Profile")
{
UserProfile *userProfile = [[UserProfile alloc] initWithNibName:#"UserProfile" bundle:nil];
userProfile.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:userProfile animated:YES];
[userProfile release];
}
else
{
ViewDetails *viewDetails = [[ViewDetails alloc] initWithNibName:#"UserAppointments" bundle:nil];
viewDetails.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: viewDetails animated:YES];
[viewDetails release];
}
}
But when I click on EditProfile label it is going to else block.
How can I recognize which Label is clicked and correspondingly perform required action?
Use Tag format like this. Which will be efficient
-(void)viewDidLoad{
lblEditProfile.userInteractionEnabled = YES;
lblEditProfile.tag = 1;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelClicked:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[lblEditProfile addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
lblViewDetails.userInteractionEnabled = YES;
lblViewDetails.tag = 2;
tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelClicked:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[lblViewDetails addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
}
-(IBAction)labelClicked:(UITapGestureRecognizer*)tapGestureRecognizer
{
currentLabel = (UILabel *)tapGestureRecognizer.view;
NSLog(#"tap %d",tapGestureRecognizer.tag);
if(currentLabel.tag == 1)
{
UserProfile *userProfile = [[UserProfile alloc] initWithNibName:#"UserProfile" bundle:nil];
userProfile.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:userProfile animated:YES];
[userProfile release];
}
else if(currentLabel.tag == 2)
{
ViewDetails *viewDetails = [[ViewDetails alloc] initWithNibName:#"UserAppointments" bundle:nil];
viewDetails.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: viewDetails animated:YES];
[viewDetails release];
}
}
Check like this:
if(currentLabel == lblEditProfile)
//code here
else
//code here

Is there a way to detect multiple pan gestures?

I want the user to be able to pan from each thumb simultaneously but I can't figure out how to detect it with uigesturerecognizer. I can detect a tap and a pan simultaneously no problem.
It appears that the second pan will block the first.
Any help is appreciated.
I solved it by defining shouldReceiveTouch like so:
-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (gestureRecognizer == singleTap) {
return YES;
}
if (gestureRecognizer == pan1 && [touch locationInView:self].x > 160) {
return YES;
}
if (gestureRecognizer == pan2 && [touch locationInView:self].x <= 160) {
return YES;
}
return FALSE;
}
And initWithFrame has the following code:
self.userInteractionEnabled = YES;
singleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
[self addGestureRecognizer:singleTap];
[singleTap release];
NSLog(#"tap: %p", singleTap);
pan1 = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePan1:)];
[self addGestureRecognizer:pan1];
NSLog(#"pan1: %p", pan1);
pan2 = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePan2:)];
[self addGestureRecognizer:pan2];
for (UIGestureRecognizer *recognizer in self.gestureRecognizers) {
recognizer.delegate = self;
}
NSLog(#"pan2: %p", pan2);

Tap gesture is not working on UIimageview

In my application i have three uiimageview which is moving randomly. on single tap on imageview it should hide. But my tapgesture is not working. on single tap it is not getting hide.
- (void)showAlert1:(UITapGestureRecognizer *)sender
{
if (image1.tag == 1)
{
image1.hidden = TRUE;
}
else
{
image1.hidden = FALSE;
}
}
- (void)showAlert2:(UITapGestureRecognizer *)sender
{
if (image1.hidden == TRUE && image3.hidden == FALSE)
{
image2.hidden = TRUE;
}
else
{
image2.hidden = FALSE;
}
}
- (void)showAlert3:(UITapGestureRecognizer *)sender
{
if (image1.hidden == TRUE && image2.hidden == TRUE)
{
image3.hidden = TRUE;
}
else
{
image3.hidden = FALSE;
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (image1.tag == 1)
{
image1.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(imageAlerts];
tap.numberOfTapsRequired = 1;
[image1 addGestureRecognizer:tap];
}
if (image2.tag == 2)
{
image2.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert2];
tap.numberOfTapsRequired = 1;
[image2 addGestureRecognizer:tap];
}
if (image3.tag == 3)
{
image3.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert3];
tap.numberOfTapsRequired = 1;
[image3 addGestureRecognizer:tap];
}
}
Can anyone help me?
Thanks in advance
Please checkmark the userInteractionEnabled and multipleTouch in xib file if you have added image in xib
or
image.userInteractionEnabled = YES;
image.multipleTouchEnabled = YES;
in ViewDidLoad
Did you implement UIGestureRecognizerDelegate and set in to self?
try this -
- (void)viewWillAppearBOOL:animated
{
[super viewWillAppear:animated];
if (image1.tag==1)
{
image1.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert1:];
tap.numberOfTapsRequired = 1;
[image1 addGestureRecognizer:tap];
}
if (image2.tag==2)
{
image2.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert2:];
tap.numberOfTapsRequired = 1;
[image2 addGestureRecognizer:tap];
}
if (image3.tag==3)
{
image3.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTargetelf actionselector(showAlert3:];
tap.numberOfTapsRequired = 1;
[image3 addGestureRecognizer:tap];
}
}
Also it has memory leak. UIGesture is not released after it adds to image.
Try this
- (void)showAlert1:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
// your handling code
if (image1.tag==1)
image1.hidden=TRUE;
else
image1.hidden=FALSE;
}
}