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

我需要知道在哪个方向上进行滑动,并知道我的视图上的哪个对象已将其发送。 我已经找到了解决方法,但它看起来很复杂和怪异,所以我怀疑是否有任何最简单的解决方案? 我的解决方法很少:

  • 我将识别器附加到我视图上的每个UIButton对象。
  • 有指示器显示在执行轻扫过程中点击了哪个按钮
  • 我必须检查这个指标和滑动方向以做出正确的决定,哪个对象和它应该移动到哪个方向。
  • 谢谢你的建议。

    @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];
        }
    
    

    我的想法是,

  • 注册UIButton的touchUpoutside事件。
  • 当你将手指从按钮上移开时,它会被触发。
  • 从touchesEnded方法中找到触点并将其存储在类级变量中。
  • 比较touchupoutside方法中的按钮框和触摸点以找出移动的方向。

  • 我认为你也可以把你的手势识别器放在self.view中,然后看看手势是否发生在控件的问题上(显然,你想让你的按钮ivars或你的viewcontroller的属性,所以你可以跟踪哪个是哪个),例如

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

    您是否在UIGestureRecognizerStateBegan或UIGestureRecognizerStateEnded或两者都执行此逻辑取决于您。

    更新:

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

    链接地址: http://www.djcxy.com/p/74239.html

    上一篇: Recognize swipe direction together with UIButton object

    下一篇: how to set cornerRadius for only top