UIGestureRecognizer in UIView Object
Solution 1
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:
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
.
Solution 2
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.
Solution 3
i think if you are working with .xib, initialization can also be done in
-(void)awakeFormNib;
Related videos on Youtube
andrew
Updated on June 04, 2022Comments
-
andrew almost 2 years
I have created a
UIViewController
, which contains aUIView
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
, andUIPinchGestureRecognizer
inside theUIView
function initWithFrame:, as it is the only entry point for theUIView
object. But the object doesn't response to theUIGestureRecognizer
objects. Why? Or, where is the best place to put theUIGestureRecognizer
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; }
-
Admin almost 13 yearsSeparate issue but you need to
release
the gesture recognizers after theaddGestureRecognizer:
calls. -
andrew almost 13 yearsThks. I am using XCode 4.2, and it uses ARC, and the compiler gives me error when I try to release/autorelease any object. I am quite mess up with what I did before with the newest compiler.
-
Admin almost 13 yearsOk, nevermind. Not used to "missing" releases yet.
-
-
Chandu about 10 yearsUIView does not haev viewDidLoad