UIScrollView with a UIButton and UITapGestureRecognizer - iphone

I've been playing around with this code, but can't get it right.
Current behaviour: I can scroll around freely on a UIScrollView, even when tapping on buttons. When i tap on something that isn't a button, it calls a method (like i want it to), as a result of a UITapGestureRecognizer. However, i have to hold down on a button for a couple of seconds then touch up for it to trigger the button's UIControlEventTouchUpInside.
What I want to happen: Exactly the same, but to be able to touch the UIButton easily, like I can if i turn off the UITapGestureRecognizer on the view it's sitting on.
Here's my code:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//...
[cell.xButton addTarget:self action:#selector(xButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.delaysContentTouches = YES;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapFrom:)];
[self addGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer.delegate = self;
}
}
- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer
{
[self.header removeKeyboard];
}

As stavash suggested, you could do the following:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
UIView* touchedView = [touch view];
if([touchedView isKindOfClass:[UIButton class]]) {
return NO;
}
return YES;
}
This works in most cases. In addition you may want to check whether the touched view is a descendent of a the scroll view using isDescendantOfView:.
Hope it helps.

I think your answer is UIGestureRecognizerDelegate's method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
Check if the touch's view property is any of the UIButtons you are trying to avoid: if so return NO, otherwise return YES. Don't forget to assign the UITapGestureRecognizer's delegate so it works. Hope this helps.

Related

gestureRecognizer:shouldReceiveTouch: implementation failing

I'm trying to ignore my gestureRecognizer if the view touched is of certain classes (namely UIButton and UIBarButtonItem) using this:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
NSLog(#"%#", touch.view);
if ([touch.view isKindOfClass:[UIButton class]]){
return NO;
} else if ([touch.view isKindOfClass:[UIBarButtonItem class]]){
return NO;
}
return YES; // handle the touch
}
That works great for UIButton. It doesn't work for UIBarButtonItem. My NSLog results when the UIBarButtonItem is touched are:
<UIToolbarTextButton: 0x7b8f500; frame = (4 0; 60 40); opaque = NO; layer = <CALayer: 0x7b8f900>> 2012-06-26 12:09:48.021
I have tried changed the [UIBarButtonItem class] to [UIToolBarTextButton class], but as this is an undocumented class, I cannot do it. Any other way to do this?
Thanks.
If your UIBarButtonItem is in a UINavigationBar, then you would need to subclass UINavigationBar and implement another
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
in order to catch the gesture for the UIBarButtonItem. There may be other ways of achieving this.

scrollview's subview does not get touch events when scroll view scrolls

I have a scrollView onto which some imageViews are added.
I want that when scrollView scrolls then also the imageViews should get touch events.
But it is not happening in my case.
Only one of the two things happen at a time.
Either the scrollView scrolls but the imageViews do not get touch events
or the imageViews get touch events and the scrollView does not get scrolled.
Can't both of the things happen simultaneously.
Please suggest.
You can use "Tap Gesture Recognizer" to get the events on your images while it's being added to the scroll view.
EDIT
You can refer to this code:
-(void)yourGestureRecognizer
{
UITapGestureRecognizer *yourTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[yourImageView addGestureRecognizer:yourGestureRecognizer];
[yourGestureRecognizer release];
}
You can handle tap here:
-(void)handleTap:(UIGestureRecognizer *)sender{
UIImageView *imageView = (UIImageView *)sender.view;
switch (imageView.tag)
{
case 1:
m_pYourInstance=[[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self.navigationController pushViewController:XXXXX animated:YES];
break;
}
Hope this helps !!
I know this was posted a while ago, but...
By default, UIImageView objects have their property userInteractionEnabled set to NO. This could be why touches aren't registering. This has tripped me up on more than one occasion and it's one of those little things that only experience will help.
Set it to YES and see if that fixes it.
Use UIGestureRecognizer. Add the recognizer to your image views.
And if you are targeting some old iOS version, have a look at this link.
yes you can do both. you need to customize your uiimageview by sub classing it and have to add Tap Gesture Recognizer like :
in .h file
#interface CustomView : UIImageView
{
// your variables
}
in .m file
//when you create an instants of your image view UITapGestureRecognizer will added in all your instants.
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
// add Gesture for tapping
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
}
return self;
}
then add delegate methods in .m file too
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
}
now in your custom cell, add your custom image view with Tap Gesture Recognizer
hope it will help you...
Use UIButtons with background images set on them. Here's the loop you use to layout UIScrollView subviews:
for (int i = 0; i < [images count]; i++) {
CGRect aFrame = ...;
UIButton *button = [[[UIButton alloc] initWithFrame:aFrame] autorelease];
[button setImage:[images objectAtIndex:i] forState:UIControlStateNormal];
button.tag = i;
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
}
And here you process the touch event:
- (void)buttonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
[self performActionWithImageAtIndex:[images objectAtIndex:button.tag]];
}
That will omit the need to create additional gesture recognizers and will lower the amount of boilerplate work.
-- edit
The other reason your UIImageViews are not detecting the touches is because you create them in code and forget to set userInteractionEnabled to YES.

Limit tap gesture to scrollview and not the button placed upon it

I have a scroll view and a button placed over it , When i add a tap gesture recognizer the button does not work. Is there any way of limiting the tap only to scroll view and not to the button, so that the button functions normally.
here is my code
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesture:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[scroll addGestureRecognizer:tap];
[tap release];
- (void)tapGesture:(UIGestureRecognizer*)gesture{
NSLog(#"scroll tapped");
}
If you do
tap.cancelsTouchesInView = NO;
It will allow button to be pressed. However taps will be detected along with the button press when you press a button. Do avoid this, you will have to subclass UIScrollView and implement the following method –
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
for ( UIView * subview in self.subviews ) {
UIView * hitView = [subview hitTest:point withEvent:event];
if ( hitView )
return hitView;
}
return [super hitTest:point withEvent:event];
}
Implementing the method above pass the touches to the scroll view's subviews.
There is no need to subclass Scrollview. Following code solves the problem easy way. The method gestureRecognizer:shouldReceiveTouch: does the trick.
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesture:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
tap.delegate = self;
tap.cancelsTouchesInView = NO;
[scroll addGestureRecognizer:tap];
[tap release];
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (scroll.superview != nil) {
if ([touch.view isKindOfClass:[UIButton class]])
{
return NO; // ignore the touch
}
}
return YES; // handle the touch
}
- (void)tapGesture:(UIGestureRecognizer*)gesture {
NSLog(#"scroll tapped");
}

I have a UITapGestureRecognizer added to an UIViewController's view that is in a UITableViewCell in a UITableView, why won't it fire?

The title is pretty explanatory, but I have a UITableView that I am populating with custom UITableViewCells.
Inside of these custom UITableViewCells, I am adding custom UIViewControllers that display custom images.
To this UIViewController's UIView, I am adding a UITapGestureRecognizer as follows:
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleTap:)];
recognizer.numberOfTapsRequired = 1;
recognizer.delegate = self;
[self.imageView addGestureRecognizer:recognizer];
[recognizer release];
}
-(void)handleTap:(UITapGestureRecognizer *)sender{
NSLog(#"Handling tap on ArticleTileViewController");
}
When I run the app, the cells are populating the images great, but when I tap on the image(or the custom UIViewController), nothing happens! My NSLog won't fire. I have looked over the code for an hour now, and don't see where I am going wrong.
Does anybody see something I'm missing? Or have they run into this before?
UIImageView objects have their userInteractionEnabled set to NO by default. This might be the case here as well. Add
self.imageView.userInteractionEnabled = YES;
to the viewDidLoad method that you've presented in the question.
If you are trying to get a button on each cell, here is a post on how to do it. Take a look.
iPhone SDK: Opening a cell with a dedicated button, not by cell tapping
- (void)viewDidLoad {
UILongPressGestureRecognizer* gesture = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPress:)] autorelease];
gesture.delegate = self;
[myWebView addGestureRecognizer:gesture];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}

Resign First Responder on ScrollView Touch

How can I hide the keyboard on ScrollView touch event...
Scenario is like that...
->View ->ScrollView ->Textfield
I want to hide the keyboard on touch of scrollView. I tried to override the class for scrollview but still i can't do it.
Doing like this will help:
#interface MyClass <UIScrollViewDelegate> {
}
#implementation
- (void)viewDidLoad {
yourScrollView.delegate = self;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[myTextField resignFirstResponder];
}
If you really want to handle the touch event, then you need to subclass the UIScrollView and override the method:
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
}
More about UIScrollView touch
This is what worked for me
in your viewController's viewDidLoad-method
UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapped)];
tapScroll.cancelsTouchesInView = NO;
[viewScroller addGestureRecognizer:tapScroll];
where viewScroller is your scroller. In the tapped-method we have
- (void) tapped {
[self.view endEditing:YES];
}
Don't know why but the above did not work for me...even though it should
Try this:
Add gesturerecognizer for scrollview,
UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped)];
tapScroll.cancelsTouchesInView = NO;
[yourScrollview addGestureRecognizer:tapScroll];
[tapScroll release];
Resign your keyboard in (tapped:) method.
Please, take a look at this answer, This is the easiest one I found.
UitextField resignFirstResponder does not work in scroll view
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
[self.view endEditing:YES];
return YES;
}