I have some trouble to handle swipe on iPhone
, I create in my interface a UISwipeGestureRecognizer
var:
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?
精彩评论