UIGestureRecognizer in UIView Object

19,512

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,

  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.

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;

Share:
19,512

Related videos on Youtube

andrew
Author by

andrew

Updated on June 04, 2022

Comments

  • andrew
    andrew almost 2 years

    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;
    }
    
    • Admin
      Admin almost 13 years
      Separate issue but you need to release the gesture recognizers after the addGestureRecognizer: calls.
    • andrew
      andrew almost 13 years
      Thks. 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
      Admin almost 13 years
      Ok, nevermind. Not used to "missing" releases yet.
  • Chandu
    Chandu about 10 years
    UIView does not haev viewDidLoad