bmende
bmende

Reputation: 741

How do you initialize a UIGestureRecognizer?

I want to add a gesture recognizer to my button so that I can run code if the user swiped past the buttons frame. I also want this code to be different if the swipe was up, right, left, or down the button.

-(void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame=CGRectMake(0, 0, 100, 100);
    [self.view addSubview:button];
    UIGestureRecognizer *swipe=[[UIGestureRecognizer alloc]initWithTarget:button action:@selector(detectSwipe)];
    [button addGestureRecognizer:swipe];
}

so, did I do the initWithTarget:action: thing correct? And now that I do this how do i Implement the detectSwipe method?

here is my idea on how to implement detectSwipe

          -(IBAction)detectSwipe:(UIButton *)sender
        {
      /* I dont know how to put this in code but i would need something like, 
if (the swipe direction is forward and the swipe is > sender.frame ){ 
[self ForwardSwipeMethod];
    } else if //same thing for right
    else if //same thing for left
    else if //same thing for down

        }

Upvotes: 1

Views: 1778

Answers (4)

Armin
Armin

Reputation: 1150

H2CO3's answer is complete. Just don't forget that you're missing a colon ":" at the end of your selector! It should be like this: @selector(detectSwipe:)

The colon ":" is because your method has an argument: (UIButton *)sender

Upvotes: 1

Craig Siemens
Craig Siemens

Reputation: 13276

You probably want to be using a UISwipeGestureRecognizer. UIGestureRecognizer usually shouldnt be used unless you are subclassing it. Your code should look similar to the following.

UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(detectSwipe)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[button addGestureRecognizer:swipe];

Upvotes: 2

user529758
user529758

Reputation:

No, it isn't correct. The target of the gesture recognizer is not the button, it's the object on which it calls the action method when detecting a gesture (otherwise how would it know on which object call that method? In OO, a method call/message send needs an explicit method name and an instance or class).

So you would most likely want

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];

You also don't create an instance of UIGestureRecognizer directly but one if its concrete subclasses, UISwipeGestureRecognizer in this case.

After alloc-initting the recognizer, you attach it to the view you want to be recognized:

[button addGestureRecognizer:recognizer];

Then in the didSwipe: method, you can use the gesture recognizer's properties to decide what the size/distance/other property of the swipe was.

You better read some docs next time.

Upvotes: 5

Eimantas
Eimantas

Reputation: 49354

You got all right except for the target of gesture recogniser. The target is an object that receives given selector message so your initWithTarget: call should accept self as an argument unless you're implementing detectSwipe method in a subclass of your button.

Upvotes: 2

Related Questions