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 usinginitWithFrame:
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:
andinitWithCoder:
. 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
.Adding the
GestureRecognizers
is typically done in theviewDidLoad
method of theUIViewController
.i think if you are working with .xib, initialization can also be done in
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:
Then, in my controller:
And it works.