Recognize swipe direction together with UIButton object

I need to recognize in which direction performed swipe and to know which object on my view has sent it. I've found workaround for now but it looks complicated and monstrous so I doubt is there any simplest solution? My workaround in few words:

  • I attached recognizers to every UIButton objects on my view.
  • There is indicator that shows which button was tapped during performing swipe
  • I have to check this indicator and swipe direction for making right decision, which object and in which direction it should be moved.
  • Thank you in advise.

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

    My idea would be,

  • Register for touchUpoutside event for UIButton.
  • When you move your finger out of the button it will be fired.
  • Find the touch point from touchesEnded method and store in a class level variable.
  • Compare the button frame and touched point in the touchupoutside method to find the direction of movement.

  • I think you could also put your gesture recognizer in self.view, and then see if the gesture took place over the control in question (obviously, you'd want to make your buttons ivars or properties of your viewcontroller so you can keep track of which is which), eg

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

    It's up to you as to whether you do this logic at UIGestureRecognizerStateBegan or UIGestureRecognizerStateEnded or both.

    Update:

    By the way, if you're detecting which direction you're moving something, you can also contemplate using UIPanGestureRecognizer, which handles all four directions in a single gesture recognizer (and because it's a continuous, you can offer the user realtime feedback in the UI animating the move).

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

    上一篇: UIButton阴影不正确

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