首页 文章

设置UISwipeGestureRecognizer的方向

提问于
浏览
129

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

它在UISwipeGestureRecognizer的文档中说明:

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

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

- (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.");
}

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

- (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.");
}

12 回答

  • 2

    如果您使用Xcode 4.2,您可以在故事板上添加Gesture Recognizers,然后将GUI手势识别器链接到IBActions .

    您可以在Utility Pane的对象库中找到Gesture Recognizers(右侧窗格的底部) .

    然后它只是一个控制问题 - 拖动到适当的行动 .

  • 25

    如果您希望它检测所有四个方向,则需要创建四个实例,就像在解决方法中所做的那样 .

    Here's Why :您创建的UISwipeGestureRecognizer的相同实例是作为发送方传递给选择器的实例 . 因此,如果您将其设置为识别所有四个方向,则对于 sgr.direction == xxx 将返回true,其中xxx是四个方向中的任何一个 .

    这是一个涉及更少代码的 alternative work-around (假设使用ARC):

    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];
    }
    
  • 5
    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [self.view addGestureRecognizer:recognizer];
    [recognizer release];
    
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [self.view addGestureRecognizer:recognizer];
    [recognizer release];
    

    现在这是didSwipe函数

    - (void) didSwipe:(UISwipeGestureRecognizer *)recognizer{
          if([recognizer direction] == UISwipeGestureRecognizerDirectionLeft){
              //Swipe from right to left
              //Do your functions here
          }else{
              //Swipe from left to right
              //Do your functions here
          }
     }
    
  • 1

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

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

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

    [更新]我从苹果公司得到了一个答案,说这个行为是按预期运作的 .

    因此,例如在表格视图中,您可以在表格视图单元格中向左或向右滑动以触发“删除”(这将具有向左和向右设置的滑动手势的方向)

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

  • 2

    我注意到左/右和上/下手势成对地一起工作,因此您只需要指定两个手势识别器 . 而且文档似乎确实是错误的 .

  • 13

    那太糟糕了,我通过添加Lars提到的两个手势解决了这个问题,并且工作得很好......

    1)左/右2)上/下

    UISwipeGestureRecognizer *swipeLeftRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
        [swipeLeftRight setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft )];
        [self.view addGestureRecognizer:swipeLeftRight];
    
        UISwipeGestureRecognizer *swipeUpDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
        [swipeUpDown setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown )];
        [self.view addGestureRecognizer:swipeUpDown];
    
  • 22

    以下是UISwipeGestureRecognizer用法的代码示例 . 注意评论 .

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        //add gesture recognizer. The 'direction' property of UISwipeGestureRecognizer only sets the allowable directions. It does not return to the user the direction that was actaully swiped. Must set up separate gesture recognizers to handle the specific directions for which I want an outcome.
        UISwipeGestureRecognizer *gestureRight;
        UISwipeGestureRecognizer *gestureLeft;
        gestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];//direction is set by default.
        gestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];//need to set direction.
        [gestureLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
        //[gesture setNumberOfTouchesRequired:1];//default is 1
        [[self view] addGestureRecognizer:gestureRight];//this gets things rolling.
        [[self view] addGestureRecognizer:gestureLeft];//this gets things rolling.
    }
    

    swipeRight和swipeLeft是用于基于向左或向右滑动执行特定活动的方法 . 例如:

    - (void)swipeRight:(UISwipeGestureRecognizer *)gesture
    {
        NSLog(@"Right Swipe received.");//Lets you know this method was called by gesture recognizer.
        NSLog(@"Direction is: %i", gesture.direction);//Lets you know the numeric value of the gesture direction for confirmation (1=right).
        //only interested in gesture if gesture state == changed or ended (From Paul Hegarty @ standford U
        if ((gesture.state == UIGestureRecognizerStateChanged) ||
        (gesture.state == UIGestureRecognizerStateEnded)) {
    
        //do something for a right swipe gesture.
        }
    }
    
  • 114
    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
        }
    
  • 2

    Swift 2.1

    我不得不使用以下内容

    for var x in [
            UISwipeGestureRecognizerDirection.Left,
            UISwipeGestureRecognizerDirection.Right,
            UISwipeGestureRecognizerDirection.Up,
            UISwipeGestureRecognizerDirection.Down
        ] {
            let r = UISwipeGestureRecognizer(target: self, action: "swipe:")
            r.direction = x
            self.view.addGestureRecognizer(r)
        }
    
  • 5

    嗯,奇怪,它对我来说很完美,我做的完全一样

    认为你应该试试看

    UIGestureRecognizerDelegate方法

    - (BOOL)gestureRecognizerShouldBegin:(UISwipeGestureRecognizer *)gestureRecognizer {
       // also try to look what's wrong with gesture
       NSLog(@"should began gesture %@", gestureRecognizer);
       return YES;
    }
    

    在日志中你必须看到类似的东西:

    应该开始做手势; target = <(action = actionForUpDownSwipeGestureRecognizer:,target =)>;方向=上,下,左,右>

  • 1

    使用这个,应该是位操作

    gesture.direction & UISwipeGestureRecognizerDirectionUp || 
       gesture.direction & UISwipeGestureRecognizerDirectionDown
    
  • 0

    这让我发疯了 . 我终于找到了一种可靠的方法来拥有多个swipeGestureRecognizer .

    如果“action”选择器的名称在多个swipeGestureRecognizer中相同,则iOS中似乎存在错误 . 如果您只是以不同的名称命名,例如handleLeftSwipeFrom和handleRightSwipeFrom,一切正常 .

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

相关问题