好吧,我有一些代码可以为视图添加4个识别器,比如这个
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
for(int d = UISwipeGestureRecognizerDirectionRight; d <= UISwipeGestureRecognizerDirectionDown; d = d*2) {
UISwipeGestureRecognizer *sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
sgr.direction = d;
[self.view addGestureRecognizer:sgr];
}
[self restore];
[self populate];
[self displaymap];
}
和像这样的识别器
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
printf("Guesture: %d\n", recognizer.direction);
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
{
printf("a\n");
[self move: 'a'];
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
printf("d\n");
[self move: 'd'];
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionUp)
{
printf("w\n");
[self move: 'w'];
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionDown)
{
printf("s\n");
[self move: 's'];
}
else if (recognizer.direction == (UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionUp))
{
printf("y\n");
[self move: 'd'];
}
}
问题是,它永远不会检测到|正确的方向,任何人都知道解决这个问题的方法吗?
答案 0 :(得分:5)
这不是direction
属性的工作方式。 UISwipeGestureRecognizer一次只能识别单向滑动。您需要做一些涉及UIPanGestureRecognizer的更复杂的事情,并根据其-translationInView:
/ -velocityInView:
方法的结果确定其方向。