与UIButton对象一起识别滑动方向

时间:2012-05-03 12:36:52

标签: objective-c ios uiswipegesturerecognizer

我需要识别在哪个方向上执行滑动并知道我视图中的哪个对象已发送它。我现在找到了解决方法,但它看起来很复杂而且很可怕,所以我怀疑有没有最简单的解决方案? 用几句话解决我的问题:

  1. 我将识别器附加到我视图中的每个UIButton对象上。
  2. 有一个指示符,显示在执行滑动期间点击了哪个按钮
  3. 我必须检查这个指标并滑动方向以做出正确的决定,哪个对象以及应该在哪个方向移动。
  4. 谢谢你的建议。

    @synthesize swipeDirection;   //label which shows swipe direction
    @synthesize buttonNumber;       //label which shows number of button which being tapped
    
        - (void)viewDidLoad
        {
            UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];     //creating button       
            [button setTitle:@"test1" forState:UIControlStateNormal];                        
            button.frame = CGRectMake(100, 100, 100, 100);
            [button addTarget:self                                          //adding selector which will update indicator which button been tapped, there isn't way to make swipe without tap button
                       action:@selector(updateButtonNumber:)
             forControlEvents:UIControlEventTouchDown];
            [button setTag:1];
            [self.view addSubview:button];              //add button to view
            [self beginListenToSwipes:button];          //send button to method which will add UISwipeGestureRecognizer for 4 directions
    
            /*same task for second button*/
            UIButton* button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];            
            [button1 setTitle:@"test2" forState:UIControlStateNormal];                        
            button1.frame = CGRectMake(200, 200, 100, 100);
            [button1 addTarget:self                         
                       action:@selector(updateButtonNumber:)
             forControlEvents:UIControlEventTouchDown];
            [button1 setTag:2];
            [self.view addSubview:button1];
            [self beginListenToSwipes:button1];
    
        }
    
        /*every time when we tap button (begin swipe this method is called and buttonNumber always points to current button*/
        -(void)updateButtonNumber:(UIButton*)sender
        {
            self.buttonNumber.text = [NSString stringWithFormat:@"%d",sender.tag];
        }
    
        /*method receives UIButton and add to it recognizers*/
        - (void) beginListenToSwipes:(UIButton*)sender
        {
            UISwipeGestureRecognizer *recognizer;
            recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
            [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
            [sender addGestureRecognizer:recognizer];
    
            recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
            [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
            [sender addGestureRecognizer:recognizer];
    
            recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
            [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
            [sender addGestureRecognizer:recognizer];
    
            recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
            [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
            [sender addGestureRecognizer:recognizer];
        }
    
        /*method which called when swipe performed and shows in which direction it was performed*/
        -(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer 
        {
            int i = recognizer.direction;
            self.swipeDirection.text = [NSString stringWithFormat:@"%d",i];
        }
    
    

2 个答案:

答案 0 :(得分:1)

我的想法是,

  1. 为UIButton注册touchUpoutside事件。
  2. 当您将手指从按钮中移开时,它会被触发。
  3. 从touchesEnded方法中找到触摸点并存储在类级别变量中。
  4. 比较touchupoutside方法中的按钮框和触摸点,以找到移动方向。

答案 1 :(得分:0)

我认为你也可以将你的手势识别器放在self.view中,然后查看手势是否发生在有问题的控件上(很明显,你想要将你的按钮设为iv或者你的viewcontroller的属性所以你可以跟踪哪个是哪个),例如

CGPoint point = [sender locationInView:self.view];
if (CGRectContainsPoint(button1.frame, point))
    // do whatever you want

您是否可以在UIGestureRecognizerStateBegan或UIGestureRecognizerStateEnded或两者中执行此逻辑。

<强>更新

顺便说一下,如果您正在检测移动某个方向的方向,您还可以考虑使用UIPanGestureRecognizer,它可以在一个手势识别器中处理所有四个方向(并且因为它是连续的,您可以在UI动画中为用户提供实时反馈。