Setting Direction for Uiswipegesturerecognizer

Setting direction for UISwipeGestureRecognizer

Seems like there is a bug. You can specify the allowed direction(s) as you did. But when you try to access the actual direction that triggered the swipe in the action selector method you still get the bit mask you originally set (for the allowed directions).

This means that checks for the actual direction will always fail when more than 1 direction is allowed.
You can see it for yourself quite easily when you output the value of 'direction' in the selector method (ie -(void)scrollViewSwiped:(UISwipeGestureRecognizer *)recognizer).

Filed a bug report (#8276386) to Apple.

[Update] I got an answer from Apple saying that the behavior works as was intended.

So for example in a table view you can swipe left or right in a table view cell to trigger 'delete' (this would have directions of the swipe gesture set to left and right)

This means that the original workaround is the way it's supposed to be used. The direction property can only be used to get the gestures recognized correctly, but not in the method performed on a successful recognition to compare for the actual direction that triggered the recognition.

Swift 4, passing UISwipeGestureRecognizer direction to the handler function

you need to create the two separate gesture for each swipe event,reason is actually works in single event, for e.g

override func viewDidLoad() {
super.viewDidLoad()

let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(moveToNextItem(_:)))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(moveToNextItem(_:)))

leftSwipe.direction = .left
rightSwipe.direction = .right

playCardView.addGestureRecognizer(leftSwipe)
playCardView.addGestureRecognizer(rightSwipe)
}

When a gesture is detected the moveToNextItem method is called, implement this method.

@objc func moveToNextItem(_ sender:UISwipeGestureRecognizer) {

switch sender.direction{
case .left:
//left swipe action
case .right:
//right swipe action
default: //default
}

}

Couldn't get UISwipeGestureRecognizer direction correctly

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];

UISwipeGestureRecognizer only one direction working

There's a couple things you should be aware of here. First, you have to create a gesture for each direction that you want to observe. This isn't a big deal though because you can simple give them the same selector, and it will be just like one gesture for two directions.

UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;

UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;


UIView *temp = [[UIView alloc]initWithFrame:self.view.frame];

temp.backgroundColor = [UIColor clearColor];
[temp addGestureRecognizer:leftSwipe];
[temp addGestureRecognizer:rightSwipe];

[self.view addSubview:temp];

Second, you never specified the direction of the gesture leaving it to default to right (or 1 on the direction enum)

From the documentation:

The default direction is UISwipeGestureRecognizerDirectionRight. See descriptions of UISwipeGestureRecognizerDirection constants for more information.

typedef enum {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
} UISwipeGestureRecognizerDirection;

How to access the 'direction' property of a UISwipeGestureRecognizer?

You cannot use one gesture recognizer to recognize two different gestures. Create two gesture recognizers, one in either direction and point them to handleSwipLeft and handleSwipeRight (that then calls handleSwipe:withDirection: or something).

How to recognize swipe in all 4 directions?

You set the direction like this

  UISwipeGestureRecognizer *Swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
Swipe.direction = (UISwipeGestureRecognizerDirectionLeft |
UISwipeGestureRecognizerDirectionRight |
UISwipeGestureRecognizerDirectionDown |
UISwipeGestureRecognizerDirectionUp);

That's what the direction will be when you get the callback, so it is normal that all your tests fails. If you had

- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender {
if ( sender.direction | UISwipeGestureRecognizerDirectionLeft )
NSLog(@" *** SWIPE LEFT ***");
if ( sender.direction | UISwipeGestureRecognizerDirectionRight )
NSLog(@" *** SWIPE RIGHT ***");
if ( sender.direction | UISwipeGestureRecognizerDirectionDown )
NSLog(@" *** SWIPE DOWN ***");
if ( sender.direction | UISwipeGestureRecognizerDirectionUp )
NSLog(@" *** SWIPE UP ***");
}

The tests would succeed (but the would all succeed so you wouldn't get any information out of them). If you want to distinguish between swipes in different directions you will need separate gesture recognizers.


EDIT

As pointed out in the comments, see this answer. Apparently even this doesn't work. You should create swipe with only one direction to make your life easier.

Problems swiping right with UISwipeGestureRecognizer (Swift 3.0)

I don't know if you dragged it on the view that should listen to the gesture. Check the links pane to see differences between them and link between the right gesture to the view
Maybe the first gesture recognizer disables the second, try to disable the first and see if it helps.. You should set them to forward touches to other recognizers

EDIT: as you said, you should've add a connection to referencing outlets
Glad I could help you (I had this problem at the past) good luck!



Related Topics



Leave a reply



Submit