Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 324
andrew Avatar asked Feb 23 '23 17:02

andrew


2 Answers

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,

  1. 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.

  2. If you want to implement this once and have a lot of interacting with the controller element, add them in viewDidLoad.

like image 71
Deepak Danduprolu Avatar answered Mar 05 '23 13:03

Deepak Danduprolu


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.

like image 38
Raphael Petegrosso Avatar answered Mar 05 '23 14:03

Raphael Petegrosso