开发者

Swipe gesture iphone

开发者 https://www.devze.com 2023-02-26 18:15 出处:网络
I have some trouble to handle swipe on iPhone, I create in my interface a UISwipeGestureRecognizervar:

I have some trouble to handle swipe on iPhone, I create in my interface a UISwipeGestureRecognizervar:

UISwipeGestureRecognizer *swipeRecognizer;

and in my controller : viewDidLoad method

- (void)viewDidLoad
{
    [super viewDidLoad];


    // Horizontal swipe
    swipeRecognizer =开发者_JS百科 [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                                                                          action:@selector(swipeMethod:)];
    swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft; 
    [self addGestureRecognizer:swipeRecognizer];
}

And my method to handle swipe is :

-(void)swipeMethod: (UISwipeGestureRecognizer *) sender
{
    NSLog(@"Swipe!");   
}

When I run my code and do a swipe I got nothing? I'm supposed to get : Swipe!

Thanks.


I'm surprised that doesn't crash with an unrecognized selector error. Try adding the recognizer to your view instead of to your view controller:

[self.view addGestureRecognizer:swipeRecognizer]


swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;

Does this work?

Don't you have to add two gestureRecognizer for each direction?

0

精彩评论

暂无评论...
验证码 换一张
取 消