ryan
ryan

Reputation: 1354

UIGestureRecognizer not working

Not sure what i'm doing wrong but here's a simplified example:

@interface Test : NSObject<UIGestureRecognizerDelegate> {
    UIView *_someParentView;
    UIView *_someChildView;
}
- (id)initWithParentView:(UIView *)parentView;
@end

@implementation Test

- (id)initWithParentView:(UIView *)parentView
{
    if (self = [super init])
    {
        _someParentView = parentView;
    }
    return self;
}

- (void)addSubViewsWhenReady
{
    _someChildView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _someChildView.backgroundColor = [UIColor blackColor];
    [_someChildView setUserInteractionEnabled:YES];
    [_someParentView addSubview:_someChildView];

    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleFingerTap.delegate = self;
    [_someChildView addGestureRecognizer:singleFingerTap];
}

- (void)handleSingleTap:(id)sender
{
    NSLog(@"handle the single tap");
}

@end

The output: "handle the single tap" is never logged. Any ideas on what im doing wrong?

Thanks!

Upvotes: 0

Views: 6060

Answers (2)

fchaubard
fchaubard

Reputation: 91

Setting the target like you are doing:

UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

is your problem. If "self" here is a UIViewController, it will work.

i.e.

@interface Test : UIViewController<UIGestureRecognizerDelegate> {
    UIView *_someParentView;
    UIView *_someChildView;
}
- (id)initWithParentView:(UIView *)parentView;
@end

@implementation Test

- (id)initWithParentView:(UIView *)parentView
{
    if (self = [super init])
    {
        _someParentView = parentView;
    }
    return self;
}

- (void)addSubViewsWhenReady
{
    _someChildView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _someChildView.backgroundColor = [UIColor blackColor];
    [_someChildView setUserInteractionEnabled:YES];
    [_someParentView addSubview:_someChildView];

    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleFingerTap.delegate = self;
    [_someChildView addGestureRecognizer:singleFingerTap];
}

- (void)handleSingleTap:(UIGestureRecognizer*)recognizer    {
    NSLog(@"handle the single tap");
}

@end

Upvotes: 3

jszumski
jszumski

Reputation: 7416

Try changing your definition of handleSingleTap: to

- (void)handleSingleTap:(UIGestureRecognizer*)recognizer {
    NSLog(@"handle the single tap");
}

From the UIGestureRecognizer docs:

A gesture recognizer has one or more target-action pairs associated with it. If there are multiple target-action pairs, they are discrete, and not cumulative. Recognition of a gesture results in the dispatch of an action message to a target for each of those pairs. The action methods invoked must conform to one of the following signatures:

- (void)handleGesture;

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

Upvotes: 1

Related Questions