UIGestureRecognizer in UIView Object

2019-06-26 21:14发布

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;
}

4条回答
疯言疯语
2楼-- · 2019-06-26 21:20

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.

查看更多
狗以群分
3楼-- · 2019-06-26 21:24

Adding the GestureRecognizers is typically done in the viewDidLoad method of the UIViewController.

查看更多
等我变得足够好
4楼-- · 2019-06-26 21:34

i think if you are working with .xib, initialization can also be done in

-(void)awakeFormNib;

查看更多
够拽才男人
5楼-- · 2019-06-26 21:39

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.

查看更多
登录 后发表回答