gesture recognizer doesn't work on subview - iphone

At first,I attach UISwipeGestureRecognizer to a image view,the action method can't be triggered.Then I attach UISwipeGestureRecognizer to view controller's view,it works well.I don't know why.
here is the code that doesn't work
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UISwipeGestureRecognizer *swipeRight=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRightAction)];
swipeRight.direction=UISwipeGestureRecognizerDirectionRight;
//imageView is an outlet of image view
[imageView addGestureRecognizer:swipeRight];
}
here is the code that works well
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UISwipeGestureRecognizer *swipeRight=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRightAction)];
swipeRight.direction=UISwipeGestureRecognizerDirectionRight;
//imageView is an outlet of image view
[self.view addGestureRecognizer:swipeRight];
}

Add this code to imageView:
imageView.userInteractionEnabled = YES;

Try this one
[imageView setUserInteractionEnabled:YES];

Related

Touching a button does not trigger a segue

Touching a button does not trigger a segue. I need to hold the button for several seconds to run the push to another screen.
I use Storyboards and IOS 5.
In IOS6 works.
Any suggestions for research?
Thanks
EDIT:
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITapGestureRecognizer *touchDismissKeyboard = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:touchDismissKeyboard];
[[self navigationController] setNavigationBarHidden:YES animated:NO];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)dismissKeyboard
{
}
The following code lines are causing the problem:
UITapGestureRecognizer *touchDismissKeyboard = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:touchDismissKeyboard];
In IOS 6 works but in IOS5 touch the button for a long time (4-5 seconds) is necessary to trigger segue
The following code lines are causing the problem:
UITapGestureRecognizer *touchDismissKeyboard = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:touchDismissKeyboard];

How to attach one UIPanGestureRecognizer object for several views?

Is it possible to alloc only one UIPanGestureRecognizer and attach to multiple views? I have multiple on-screen UIImageView objects that user can move (pan). If I'm making alloc and attaching the same UIPanGestureRecognizer object to all views then the pan gesture works only on the last attached view. I did a workaround by making multiple alloc/init of UIPanGestureRecognizer, I mean one different UIPanGestureRecognizer object for each view. Here's the code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer * pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(updateImagePosition:)];
[self.panningBtn1 addGestureRecognizer:pgr];
[pgr release];
pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(updateImagePosition:)];
[self.panningBtn2 addGestureRecognizer:pgr];
[pgr release];
...
PS. I also have enabled MultipleTouchEnabled & UserInteractionEnabled. Are there any more elegant solutions?
No - each UIGestureRecognizer can only belong to one view. Just create multiple with the same properties and assign them to the different views.
I suggest creating the gesture recognizer in a method:
-(UIGestureRecognizer *)myGRMethod {
UIPanGestureRecognizer *pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(updateImagePosition:)];
return [pgr autorelease];
}
and then just doing
for (UIView *view in views) {
[view addGestureRecognizer:[self myGRMethod]];
}

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;
}

App freeze when I remove [super loadView]

When I remove the [super loadView]; the view wont display. The superclass is UIViewController.
- (void)loadView
{
[super loadView];
UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 367.0f) style:UITableViewStylePlain];
tableview.dataSource = self;
tableview.delegate = self;
self.tableView = tableview;
[tableview release];
[self.view addSubview:self.tableView];
}
Any idea why? Thanks in advance!
1) UIViewController Class Reference, loadView section
Your custom implementation of this method should not call super.
2) You have to set view property to something. After all this method is called loadView :). Instead of [self.view addSubview:self.tableView]; try
self.view = tableView;
If you look at the view programming guide, it mentions that if you override [loadView], you should construct your own view.
default loadView will look at bunch of stuff, like load from nib first, then construct normal view.
So, just construct a view, and assign it to self.view -
UIView *view = [[UIView alloc] initWithFrame ...];
self.view = view;
[view release];
then it should be fine.
edit: example with your code:
- (void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 367.0f)];
self.view = view;
[view release];
UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 367.0f) style:UITableViewStylePlain];
tableview.dataSource = self;
tableview.delegate = self;
self.tableView = tableview;
[tableview release];
[self.view addSubview:self.tableView];
}
edit2: link to viewcontroller programming guide:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html#//apple_ref/doc/uid/TP40007457-CH101-SW1
Look at custom view controller section, Creating the View Programmatically, and few other places in that doc.
I think you wanna move that [ tableview release ] after the addSubview.
This is because you never assign the view property in the code. When in the last line you access the view property, it causes -loadView to be called again, which results in a endless loop.

UIBarButton not working when toolbar superview has UITapGestureRecognizers?

I am experiencing this strange behavior.
I create a clean project (view template), add a toolbar with a button and hook it up with an action. it works ;)
BUT, then when I add a UITapGestureRecognizer to the view of my viewcontroller the toolbar button stops working. (It is pressed but its action is not called)
When I add the UITapGestureRecognizer only the action linked to it is called. It is like uitapgesture recognizer view were hiding the toolbar but actually is not.
What is happening here? What am I missing?
- (IBAction)itemAction{
NSLog(#"%s", _cmd);
self.view.backgroundColor = [UIColor whiteColor];
}
- (void) tapAction{
NSLog(#"%s", _cmd);
self.view.backgroundColor = [UIColor greenColor];
}
- (void)viewWasTapped:(UITapGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateRecognized) {
[self tapAction];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(viewWasTapped:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
}
EDIT: the project source can be downloaded from here
Thanks in advance for any advice
Ignacio
I've encountered a similar problem, and found this answer quite helpful, especially because it allows selectively excluding the gesture recognizer based on the type of view that was touched.
Eventually I found the reason:
tapGestureRecognizer.cancelsTouchesInView = NO;