Sparklellama
Sparklellama

Reputation: 720

UIGestureRecognizer Delegate method is never called

Trying to make some draggable images in a subview of a scrollview. But nothing happens. Anybody got any ideas why a breakpoint in handlePan is never hit? :'( Thanks if u can help!

-(void) viewWillAppear:(BOOL)animated
{
    twView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"twitter"]];
    [self.springView addSubview:twView];
    [twView setFrame:CGRectMake(100, 100, 60, 60)];

    fbView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"facebook"]];
    [self.springView addSubview:fbView];
    [fbView setFrame:CGRectMake(200, 200, 60, 60)];

    g1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [twView addGestureRecognizer:g1];
    g1.delegate = self;

    g2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [fbView addGestureRecognizer:g2];
    g2.delegate = self;

}

-(void)handlePan:(UIPanGestureRecognizer *)gesture
{
    ... do amazing things
}

Upvotes: 1

Views: 5102

Answers (3)

Mr.Fingers
Mr.Fingers

Reputation: 1144

Another thing that helps to me, is on instance of gesture recognizer set

UITapGestureRecognizer *loginTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loginButtonAction)];
loginTap.delegate = self;
[self.loginButton setUserInteractionEnabled:YES];
[self.loginButton addGestureRecognizer:self.loginTap];

And after assigning delegate to self, it will receive call in delegate methods

Upvotes: 1

Earl Grey
Earl Grey

Reputation: 7466

The built-in pan gesture recogniser of UIScrollView subclass is probably intercepting the gesture.

Make sure your view controller implements the UIGestureRecognizerDelegate protocol and implement

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES; 
}

Upvotes: 2

InkGolem
InkGolem

Reputation: 2762

Based on what you have here, it's probably one of two things. Either your image view doesn't have userInteractionEnabled set to yes, or there is another view on top of your image views blocking the gestures.

Upvotes: 3

Related Questions