James Raitsev
James Raitsev

Reputation: 96391

On UIGestureRecognizers, clarification needed

Other then setting UIGestureRecognizers in a Controller:

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] 
               initWithTarget:v action:@selector(handleSwipe:)];
swipeGesture.direction= UISwipeGestureRecognizerDirectionUp;
[v addGestureRecognizer:swipeGesture];

And making a method in a view available

-(void)handleSwipe:(UISwipeGestureRecognizer *)sender {
    NSLog(@"Swipe detected");
}

Is there anything else needed? If not, what am i missing please? When gesture is simulated, NSlog is not printing

Upvotes: 0

Views: 118

Answers (2)

NJones
NJones

Reputation: 27147

Edit

I see from your recent question that you are also using a pan gesture recognizer. Gesture recognizers don't really know how to play well with each-other without instruction. You have two main options

1) One or the other:

[panGesture requireGestureRecognizerToFail:swipeGesture];

The pan only fires if the touch is not a swipe. or-

2) Both at the same time:

Conform the the UIGestureRecognizerDelegate protocol.

Set the delegates:

panGesture.delegate = self;
swipeGesture.delegate = self;

And implement the method:

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

Original

Right now you are setting your view v as the target of the recognizer, so essentially when the swipe is detected the recognizer calls:

[v handleSwipe:self];

It seems more likely that both of your methods are in a view controller. If that's the case then the init method should look like this:

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] 
           initWithTarget:self action:@selector(handleSwipe:)];

Since v is the intended target and with that in mind your code is fine; I would think the next most likely cause is that your first chunk of code runs before the view v loads. i.e. the code is not in viewDidLoad after v already exists. If you're unsure a simple log will confirm it's existence:

NSLog(@"v is %@",v);

If that proves to be a dead-end we will definitely require more info on this view v. What is it's superclass? Are there other recognizers on it? How is it added to the view?

Upvotes: 2

dasdom
dasdom

Reputation: 14063

Check whether the view accepts user interaction. This can be done in storyboard or via [myView setUserInteractionEnabled: YES].

Upvotes: 2

Related Questions