Setting delegate for UIGestureRecognizer giving warning - ios

setting delegate for UITapGesture giving warning
Here is my code :
UITapGestureRecognizer *tapOtherPlayers = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapcollectionViewOtherPlayers:)];
tapOtherPlayers.delegate = self;
[tapOtherPlayers setNumberOfTapsRequired:1];
[collectionViewOtherPlayers addGestureRecognizer:tapOtherPlayers];

You need to add UIGestureRecogniserDelegate in your .h file as following :
Hope it helps..

Make "tapOtherPlayers" Variable as global and set UIGestureRecognizerDelegate delegate as bellow.
#interface ViewController ()<UIGestureRecognizerDelegate>{
UITapGestureRecognizer *tapOtherPlayers;
}
#end
#implementation ViewController
- (void)viewDidLoad {
tapOtherPlayers = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapcollectionViewOtherPlayers:)];
tapOtherPlayers.delegate = self;
[tapOtherPlayers setNumberOfTapsRequired:1];
[collectionViewOtherPlayers addGestureRecognizer:tapOtherPlayers];
}
Hope this will help you...

Related

UITapGestureRecognizer is nil

I have a table where a gesturerecognizer should be added to the table-header-label. This is done through following code:
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleTableheaderTap:)];
[headerView addGestureRecognizer:singleFingerTap];
singleFingerTap.delegate = self;
The selector-method looks like following:
- (void)handleTableheaderTap:(UITapGestureRecognizer *)recognizer
Unfortunately, the recognizer is nil, when the method is called. What do I have to change, so the recognizer is in the handletTableheaderTap not nil anymore?
Thank you!
You have to store the instance in a controller attribute.
Declare a private property to store it.
Example :
#interface MyViewController()
#property (nonatomic, strong) UITapGestureRecognizer *singleFingerTap
#end
...
self.singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleTableheaderTap:)];
[headerView addGestureRecognizer:singleFingerTap];
singleFingerTap.delegate = self;

use class methods for encapsulation?

i have made some #properties of uiimage view type in a class and then initialized that class in my rootviewcontroller.
questions.h:
#property(nonatomic,strong)UIImageView *img1;
#property(nonatomic,strong)UIImageView *img2;
#property(nonatomic,strong)UIImageView *img3;
#property(nonatomic,strong)UIImageView *img4;
'viewcontroller.m'
-(void)viewdidload{
questions *q1=[[questions alloc]init];
q1.img1 =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"red.png"]];
[self.view addSubview:q1.img1];
UITapGestureRecognizer *Tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(iTap:)];
[q1.img1 addGestureRecognizer:Tap1];
}
-(void)iTap:(UIGestureRecognizer *)gestureRecognizer {
// do something..
}
i know its not a good idea to populate viewdidload with initialization code..
is there any better way to acive this using class methods ?
like :
questions.m:
+(questions * )mymethod{
questions *q1=[[questions alloc]init];
q1.img1 =[[uiimageview alloc]initwithimage [uimage imagenamed#"red.png"]];
[self.view addSubview:q1.img1];
UITapGestureRecognizer *Tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(iTap:)];
[q1.img1 addGestureRecognizer:Tap1];
return q1;
}
and then calling that method in viewdidload
viewcontroller.m:
-(void)viewdidload{
[questions mymethod];
}
There's nothing really wrong with creating adding views in viewDidLoad. You can however also use - [UIViewController loadView].

trying to add a UITapGestureRecognizer but not working

I have tried setting up a simple UITapGestureRecognizer on a UILabel but it is not working. Nothing seems to be recognized and nothing is written to the log.
in a method called from viewDidLoad
UILabel *miTN = [[UILabel alloc] initWithFrame:CGRectMake(300, 100, 150, 15)];
[miTN setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized)];
[miTN addGestureRecognizer: tapRecognizer];
[miView addSubview:miTN];
... later
- (void)tapRecognized:(id)sender
{
NSLog(#"that tap was recognized");
}
Also, this is called in response to an async network call. Could that be causing the problem? Is there some other issue that might be causing isssue? I'm not really sure what first step to debug would be - I checked Color Blended Layers to see if they were ovrelapping but don't appear to be.
You should add UIGestureRecognizerDelegate to the protocol list for your viewcontroller class
#interface TSViewController : UIViewController <UIGestureRecognizerDelegate>
and insert string:
tapRecognizer.delegate = self;
and replace
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized)];
to
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized:)];
in Swift, you have to enable by doing this as follows.
yourLabel.isUserInteractionEnabled = true

How do I get UIGestureRecognizers to work?

I'm having trouble just turning on gesture recognizers in my code. I added a handleTap callback to to the tap gesture recognizer but the print statement never happens. I'm just not having any luck. Does anyone see what I'm doing wrong here?
In my ViewController.h
#interface ViewController : UIViewController<UITextFieldDelegate, UIGestureRecognizerDelegate> {
}
#end
This is what I have in my ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
View *myview = [[View alloc] initWithFrame:applicationFrame];
myview.userInteractionEnabled = YES;
myview.multipleTouchEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleTap)];
tap.numberOfTapsRequired = 1;
tap.delegate = self;
[myview addGestureRecognizer:tap];
self.view = myview;
}
-(void)handleTap:(UITapGestureRecognizer*)recognizer {
NSLog(#"dismiss keyboard");
//[usernameTextField resignFirstResponder];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return YES;
}
Edit: I added a text field and when I click on the text field I see the tap selector print statement. But if I tap off the text field I don't get the selector print statement.
I think the problem is with your selector. Try changing
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleTap)];
And replace it with
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleTap:)];
To make the selector match the signature for handleTap.
You are passing the wrong selector when initialising.
The line should read like this:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleTap:)];
Notice the colon after handleTap to match the method you want to use.
Apparently subclassing the UIView and overriding drawrect with an empty function caused the selector to be called.

UIGestureRecognizer in UIView Object

I have created a UIViewController, which contains a UIView object.
I want the UIView object response to a single tap by 1 finger, and also pinch by 2 fingers.
I have clicked the "User Interaction Enabled" and the "Multiple touch" options in the xib.
I create both UITapGestureRecognizer, and UIPinchGestureRecognizer inside the UIView function initWithFrame:, as it is the only entry point for the UIView object. But the object doesn't response to the UIGestureRecognizer objects. Why? Or, where is the best place to put the UIGestureRecognizer objects?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
// single tap
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self addGestureRecognizer: singleTap];
// 2 fingers pinch
UIPinchGestureRecognizer* doubleMove = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleMove:)];
[self addGestureRecognizer: doubleMove];
}
return self;
}
Your problem is that when you instantiate your custom view subclass in IB, initWithCoder: is called. If you had done it programmatically using initWithFrame: it would have worked properly.
Two ways you can fix this,
Move the gestures setup to a different method and call them in both initWithFrame: and initWithCoder:. I would recommend doing this if you intend to reuse this component and expose some kind of callbacks on gestures.
If you want to implement this once and have a lot of interacting with the controller element, add them in viewDidLoad.
I tried your code and it works for me.
Where are you setting the view? Maybe it has any other view in front, or its superview has userInteractionEnabled disabled.
I created a UIView subclass and added this code:
-(void) handleSingleTap:(UITapGestureRecognizer *)gr {
NSLog(#"handleSingleTap");
}
-(void) handleDoubleMove:(UIPinchGestureRecognizer *)gr {
NSLog(#"handleDoubleMove");
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
// single tap
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self addGestureRecognizer: singleTap];
// 2 fingers pinch
UIPinchGestureRecognizer* doubleMove = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleMove:)];
[self addGestureRecognizer: doubleMove];
}
return self;
}
Then, in my controller:
-(void) viewDidLoad {
[super viewDidLoad];
MyView * myView = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
myView.backgroundColor = [UIColor greenColor];
[self.view addSubview:myView];
[myView release];
}
And it works.
i think if you are working with .xib, initialization can also be done in
-(void)awakeFormNib;
Adding the GestureRecognizers is typically done in the viewDidLoad method of the UIViewController.

Resources