Combine longpress gesture and drag gesture together

后端 未结 5 753
执笔经年
执笔经年 2020-12-07 20:37

I\'m moving my views by

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveRight:)];
[panRecogn         


        
相关标签:
5条回答
  • 2020-12-07 20:56
    @implementation MyViewController {
        CGPoint _priorPoint;
    }
    
    - (void)moveRight:(UILongPressGestureRecognizer *)sender {
        UIView *view = sender.view;
        CGPoint point = [sender locationInView:view.superview];
        if (sender.state == UIGestureRecognizerStateChanged) {
            CGPoint center = view.center;
            center.x += point.x - _priorPoint.x;
            center.y += point.y - _priorPoint.y;
            view.center = center;
        }
        _priorPoint = point;
    }
    
    0 讨论(0)
  • 2020-12-07 21:07

    You do not need to declare _priorPoint;

    In my case, i only want the view to move horizontally so i'm only changing the x coordinate.

    Here is my solution:

        if (longpressGestRec.state == UIGestureRecognizerStateChanged)
        {
            UIView *view = longpressGestRec.view;
    
            // Location of the touch within the view.
            CGPoint point = [longpressGestRec locationInView:view];
    
            // Calculate new X position based on the amount the gesture
            // has moved plus the size of the view we want to move.
            CGFloat newXLoc = (item.frame.origin.x + point.x) - (item.frame.size.width / 2);
            [item setFrame:CGRectMake(newXLoc,
                                      item.frame.origin.y,
                                      item.frame.size.width,
                                      item.frame.size.height)];
        }
    
    0 讨论(0)
  • 2020-12-07 21:15

    UILongPressGestureRecognizer already does what you want for you. Take a look at the UIGestureRecognizerState property. From the documentation:

    Long-press gestures are continuous. The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted.

    So essentially after your UILongPressGestureRecognizerselector is called you listen to UIGestureRecognizerStateBegan, UIGestureRecognizerStateChanged, UIGestureRecognizerStateEnded. Keep changing your views frame during UIGestureRecognizerStateChanged.

    - (void)moveRight:(UILongPressGestureRecognizer *)gesture
    {
        if(gesture.state == UIGestureRecognizerStateBegan)
        {
            //if needed do some initial setup or init of views here
        }
        else if(gesture.state == UIGestureRecognizerStateChanged)
        {
            //move your views here.
            [yourView setFrame:];
        }
        else if(gesture.state == UIGestureRecognizerStateEnded)
        {
            //else do cleanup
        }
    }
    
    0 讨论(0)
  • 2020-12-07 21:21

    Thanks to Hari Kunwar for the Swift code, but the longPressAction function is not correctly defined.

    Here's an improved version:

    @objc func longPressAction(gesture: UILongPressGestureRecognizer)  {
        if gesture.state == UIGestureRecognizerState.began {
        }
        else if gesture.state == .changed {
            guard let view = gesture.view else {
                return
            }
            let location = gesture.location(in: self.view)
            view.center = CGPoint(x:view.center.x + (location.x - view.center.x),
                                              y:view.center.y + (location.y - view.center.y))
        }
        else if gesture.state == UIGestureRecognizerState.ended{
        }
    }
    
    0 讨论(0)
  • 2020-12-07 21:22

    In Swift this can be achieved using below code

    class DragView: UIView { 
      // Starting center position
      var initialCenter: CGPoint?
    
      override func didMoveToWindow() {
        super.didMoveToWindow()
        // Add longPress gesture recognizer
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction(gesture:)))
        addGestureRecognizer(longPress)
      }
    
      // Handle longPress action
      func longPressAction(gesture: UILongPressGestureRecognizer) {
        if gesture.state == .began {
            guard let view = gesture.view else {
                return
            }
            initialCenter = gesture.location(in: view.superview)
        }
        else if gesture.state == .changed {
            guard let originalCenter = initialCenter else {
                return
            }
    
            guard let view = gesture.view else {
                return
            }
    
            let point = gesture.location(in: view.superview)
    
            // Calculate new center position
            var newCenter = view.center;
            newCenter.x += point.x - originalCenter.x;
            newCenter.y += point.y - originalCenter.y;
    
            // Update view center
            view.center = newCenter
        }
        else if gesture.state == .ended {
           ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题