您的位置:首页 > 产品设计 > UI/UE

Get swipe direction in Cocoa Touch:UISwipeGestureRecognizer只能同时支持一个方向

2012-11-27 13:57 495 查看
I am trying to catch a gesture but it does not work. Here is my code:
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];


and
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"get gesture");
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"get gesture right");
}
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"get gesture Left");
}
}


It always gets a gesture but does not recognize the direction. I also tried
if(recognizer.direction){NSLog(@"get
gesture");}
and it also worked, so I do not understand where I made the mistake.

Thanks for any help.

19down
voteaccepted
You're not using the
UISwipeGestureRecognizer
correctly.
Its direction is always going to be what you've set it to (in this case
UISwipeGestureRecognizerDirectionRight
| UISwipeGestureRecognizerDirectionLeft
, or 3).

If you want to capture swipes left and right that you can differentiate between, you'll have to set up a separate recognizer for each. Apple does this in their SimpleGestureRecognizers
sample.

19down
voteaccepted
You're not using the
UISwipeGestureRecognizer
correctly.
Its direction is always going to be what you've set it to (in this case
UISwipeGestureRecognizerDirectionRight
| UISwipeGestureRecognizerDirectionLeft
, or 3).

If you want to capture swipes left and right that you can differentiate between, you'll have to set up a separate recognizer for each. Apple does this in their SimpleGestureRecognizers
sample.

3down
vote
What you have to do is just change the codes for adding gesture recognizer.
UISwipeGestureRecognizer *leftRecognizer;
leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[leftRecognizer setDirection: UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:leftRecognizer];
[leftRecognizer release];

UISwipeGestureRecognizer *rightRecognizer;
rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[rightRecognizer setDirection: UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:rightRecognizer];
[rightRecognizer release];


19down
voteaccepted
You're not using the
UISwipeGestureRecognizer
correctly.
Its direction is always going to be what you've set it to (in this case
UISwipeGestureRecognizerDirectionRight
| UISwipeGestureRecognizerDirectionLeft
, or 3).

If you want to capture swipes left and right that you can differentiate between, you'll have to set up a separate recognizer for each. Apple does this in their SimpleGestureRecognizers
sample.

3down
vote
What you have to do is just change the codes for adding gesture recognizer.
UISwipeGestureRecognizer *leftRecognizer;
leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[leftRecognizer setDirection: UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:leftRecognizer];
[leftRecognizer release];

UISwipeGestureRecognizer *rightRecognizer;
rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[rightRecognizer setDirection: UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:rightRecognizer];
[rightRecognizer release];


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: