UITapGestureRecognizer action not triggered - iphone

I have an UIImageView inside a subclass of UITableViewCell in which I wired up with UITapGestureRecognizer. However, when I press on the image the action is not triggered. What's being triggered is the didSelectRowAtIndexPath.. why is this?
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[imageView addGestureRecognizer:tapGesture]; [tapGesture release];
Also one minor question is that I have a UITextView in the cell, however when I click on the text it did not trigger the didSelectRowAtIndexPath, why is this and how do I change it?

UIImageView has userInteractionEnabled set to NO by default. Try setting it to YES
imageView.userInteractionEnabled = YES;
For your other question, try setting the editable property to NO for the UITextView.
textView.editable = NO;

Related

Gesture recognizer does not recognise a view which is created over an UIImageView

I have an UIImageview with tap gesture on it, such that any tap over it creates a rectangular view with white background. Which I can move any where over imageview. Like this I can create infinite views over that imageview.
Now my problem is that I want to add double tap gestures to newly created views but when I click on any of those views then a new view is created over that view.
But what I want is that if I tap on a view then no new view must be created over that view.
Currently gestureRecognizer recognises UIImageView even if I have created a new view over UIImageView.
ImageView,
UIImageView *imageView = [[UIImageView alloc]init];
UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(createNewView)];
[tapRecognizer setNumberOfTapsRequired:1]; // allow to create new view
UITapGestureRecognizer * tapRecognizerToFail = [[UITapGestureRecognizer alloc]init];
[tapRecognizer setNumberOfTapsRequired:2]; // to disallow more than 1 tap.
[tapRecognizer requireGestureRecognizerToFail:tapRecognizerToFail];
[imageView setGestureRecognizers:[NSArray arrayWithObjects:tapRecognizer,tapRecognizerToFail,nil]];
// New View Creation over ImageView
-(void)createNewView
{
UIView * view = [[UIView alloc]init];
UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(newViewAction)];
[tapRecognizer setNumberOfTapsRequired:2];
[view addGestureRecognizer:tapRecognizer];
[imageView addSubview:view];
}
// Double tap action over newly created view
-(void)newViewAction
{
// goes here
}
You can use requireGestureRecognizerToFail: to declare a dependency between your two recognizers.
[secondaryGesture requireGestureRecognizerToFail:primaryGesture];

Many tap gesture in on view

I have problem with tap gesture. My case is:
- A view with UITapGestureRecognizer to dismiss keyboard
- A label on view, which has 2 gesture. One UITapGestureRecognizer to open popup, and UITapGestureRecognizer (number of touches is 2) to quickly confirm popup.
But when I tap on label, the UITapGestureRecognizer on view always receive the action. How can I forward action to UILabel.
Thanks
To get taps on label, you need to enable user interaction for that label
To receive 2 taps you need to do following
In short [tap requireGestureRecognizerToFail:dTap]; will do trick for you making single tap to wait for a while to check if double tap is to happen
UITapGestureRecognizer *dTap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(doubleTapped:)];
dTap.delegate = self;
dTap.numberOfTapsRequired = 2;
dTap.numberOfTouchesRequired = 1;
[label addGestureRecognizer:dTap];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(tapped:)];
tap.delegate = self;
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[label addGestureRecognizer:tap];
[tap requireGestureRecognizerToFail:dTap];
// it Enables user interaction on you Lable, by default its NO, so you have to do it.
[lbl setUserInteractionEnabled:YES];

Can UIView and UIImagView implement touch events simultaneously?

In the UIView, there is several sub views of UIImageView objects.
Since I need to detect the touch events from the UIImageView and from the remaining area of the UIView.
Can I implement touch events in UIView and UIImagView simultaneously ?
Thanks.
Based on your comment, I think you should consider gesture recognizers. Use the UITapGestureRecognizer to identify and respond to taps on a view.
UITapGestureRecognizer *tapGesture;
tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapOnView:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[view addGesture:tapGesture];
[tapGesture release];
...
tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapOnImage:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[imageView addGesture:tapGesture];
[tapGesture release];
...
define -(void)handleTapOnView:(UITapGestureRecognizer*)gesture and -(void)handleTapOnImage:(UITapGestureRecognizer*)gestureand handle the touches there.
There are other kinds of gestures too. You can read more about them here.

UILongPressGestureRecognizer of Superview gets called instead of Subview

i got a strange problem. I got a Superview with customcontrols as subviews.
Their are GestureRecognizer both in superview and subviews.
If i tap a subview its GestureRecognizer gets called and on tap on the superview its tap gets called.
but on long press in the subview SOMETIMES the GestureRecognizer of the superview gets called. I add the GestureRecognizers in the same functions but there is a different attitude.
Superview
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapPiece:)];
tapGesture.numberOfTapsRequired = 1;
//contenView is the area where my controls can be
[self.contentView addGestureRecognizer:tapGesture];
[tapGesture release];
UILongPressGestureRecognizer *longTapGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longTapPiece:)];
tapGesture.numberOfTapsRequired = 1;
[self.contentView addGestureRecognizer:longTapGesture];
[longTapGesture release];
Subviews
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(selectPiece:)];
tapGesture.numberOfTapsRequired = 1;
[self addGestureRecognizer:tapGesture];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPiece:)];
longPressGesture.numberOfTapsRequired = 1;
[self addGestureRecognizer:longPressGesture];
Can anyone tell me why the longtap doen't response to my subview and how to fix it.
THANK YOU
got a solution but this is not what i wanted i set the duration of the control lower than the one of the superview [longPressGesture setMinimumPressDuration:0.4]; But the gestureRecognizer should be independend

Detecting taps in uiimageview inside uiscrollview

I have a UIScrollView with multiple UIImageViews in it created like this.
frame = [[UIImageView alloc] initWithImage:bg];
frame.frame = CGRectMake(FRAME_SEPARATOR + numPage*1024 + numColumn*(FRAME_SEPARATOR+230), 10 +numRow*(FRAME_SEPARATOR+145), 230, 145);
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[frame addGestureRecognizer:tap];
[tap release];
[scroll addSubView:frame];
The problem is that imageTapped is not being called when tapping over an image.
If I add the gesture recognizer to the scrollview like this:
UITapGestureRecognizer *tap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[scroll addGestureRecognizer:tap];
[tap release];
imageTapped is called.
How can I detect the taps over the UIImageViews?
Thanks
Make sure userInteractionEnabled is set to YES on the UIImageView:
frame.userInteractionEnabled = YES;
I'd also recommend using a different name for the UIImageView variable (eg. imageView instead of frame). Otherwise, you can easily confuse it with the view's frame property.