设置UISwipeGestureRecognizer的方向 [英] Setting direction for UISwipeGestureRecognizer

查看:162
本文介绍了设置UISwipeGestureRecognizer的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在基于视图的iPhone项目中添加简单的滑动手势识别功能。应识别所有方向(右,下,左,上)的手势。

I want to add simple swipe gesture recognition to my view based iPhone project. Gestures in all directions (right, down, left, up) should be recognized.

UISwipeGestureRecognizer的文档中说明:

It is stated in the docs for UISwipeGestureRecognizer:


您可以通过使用按位或操作数指定多个UISwipeGestureRecognizerDirection常量来指定多个方向。默认方向是UISwipeGestureRecognizerDirectionRight。

You may specify multiple directions by specifying multiple UISwipeGestureRecognizerDirection constants using bitwise-OR operands. The default direction is UISwipeGestureRecognizerDirectionRight.

但对我来说它不起作用。当所有四个方向都被或时,只识别左右滑动。

However for me it doesn't work. When all four directions are OR'ed only left and right swipes are recognized.

- (void)viewDidLoad {
    UISwipeGestureRecognizer *recognizer;

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)];
    [[self view] addGestureRecognizer:recognizer];
    [recognizer release]; 
    [super viewDidLoad];
}

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"Swipe received.");
}

我通过在视图中添加四个识别器来修复此问题,但我很好奇知道为什么它不像在文档中宣传的那样起作用?

I fixed this with adding four recognizers to the view but I'm curious to know why didn't it work as advertised in docs?

- (void)viewDidLoad {
    UISwipeGestureRecognizer *recognizer;

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

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [[self view] addGestureRecognizer:recognizer];
    [recognizer release];

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [[self view] addGestureRecognizer:recognizer];
    [recognizer release];

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [[self view] addGestureRecognizer:recognizer];
    [recognizer release];

    [super viewDidLoad];
}

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"Swipe received.");
}


推荐答案

好像有一个bug 。您可以像指定的那样指定允许的方向。但是当您尝试访问动作选择器方法中触发滑动的实际方向时,您仍然会获得最初设置的位掩码(对于允许的方向)。

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).

这意味着当允许超过1个方向时,对实际方向的检查将始终失败。
当您在选择器方法中输出'direction'的值时,您可以很容易地看到它(即 - (void)scrollViewSwiped:(UISwipeGestureRecognizer *)识别器)。

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).

向Apple提交了一份错误报告(#8276386)。

Filed a bug report (#8276386) to Apple.

[更新]我得到了一个来自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)

这意味着原始的解决方法是它应该使用的方式。 direction属性只能用于正确识别手势,但不能用于成功识别时执行的方法,以比较触发识别的实际方向。

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.

这篇关于设置UISwipeGestureRecognizer的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆