问题
I'm writing code to move my two fingers up or down on a view to change some status. The code as below:
UISwipeGestureRecognizer *aSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
[aSwipeGesture setDirection:UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown];
aSwipeGesture.numberOfTouchesRequired = 2;
[self.View addGestureRecognizer:aSwipeGesture];
- (void)swipeGesture:(UISwipeGestureRecognizer *)sender {
NSLog(@"Swipe received.");
if (sender.direction==UISwipeGestureRecognizerDirectionUp) {
NSLog(@"swipe up");
} else if (sender.direction==UISwipeGestureRecognizerDirectionDown) {
NSLog(@"swipe down");
}
}
However the only print log I could receive was Swipe received as below shows. I couldn't get the message for swipe up or swipe down, did I miss anything? Thanks
ViewController.m:228 Swipe received.
ViewController.m:228 Swipe received.
ViewController.m:228 Swipe received.
ViewController.m:228 Swipe received.
ViewController.m:228 Swipe received.
ViewController.m:228 Swipe received.
Updated: I have to use two fingers to finish the swipe action.
回答1:
Try this
UISwipeGestureRecognizer *aSwipeGestureUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
[aSwipeGesture setDirection:UISwipeGestureRecognizerDirectionUp];
aSwipeGestureUp.numberOfTouchesRequired = 2;
[self.View addGestureRecognizer:aSwipeGestureUp];
UISwipeGestureRecognizer *aSwipeGestureDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
[aSwipeGestureDown setDirection:UISwipeGestureRecognizerDirectionDown];
aSwipeGestureDown.numberOfTouchesRequired = 2;
[self.View addGestureRecognizer:aSwipeGestureDown];
回答2:
sadly the API makes no sense at all, you can SET a UISwipeGestureRecognizer to recognize a swipe in any of the 4 directions, but it will not tell you what direction the swipe happened in.
they were careful enough to allow the 4 constants to be used together (it's a bitmap) but the API is just broken.
you will have to use one recognizer for each direction, a shame !
回答3:
In your case you have set Direction of swipe as UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown . So in delegate callback you will receive same value for sender.direction, that is why the logs are not printed as the enum value is not matching with sender.direction value.
If you want to handle separately the up and down swipe, you need to create 2 swipe gestures with up and down swipe directions and add to your view. Then based on the direction of sender do the task.
回答4:
UISwipeGestureRecognizer *Updown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleGestureNext:)]; Updown.delegate=self; [Updown setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp]; [overLayView addGestureRecognizer:Updown];
UISwipeGestureRecognizer *LeftRight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleGestureNext:)];
LeftRight.delegate=self;
[LeftRight setDirection:UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight];
[overLayView addGestureRecognizer:LeftRight];
overLayView.userInteractionEnabled=NO;
-(void)handleGestureNext:(UISwipeGestureRecognizer *)recognizer
{
NSLog(@"Swipe Recevied");
//Left
//Right
//Top
//Bottom
}
回答5:
remove aSwipeGesture.numberOfTouchesRequired = 2; and try
来源:https://stackoverflow.com/questions/16184539/couldnt-get-uiswipegesturerecognizer-direction-correctly