Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine longpress gesture and drag gesture together

I'm moving my views by

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveRight:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[bubbleView[rightCnt] addGestureRecognizer:panRecognizer];
[panRecognizer release];

Now , I want to do same thing by drag with long press.

Any idea?

like image 219
PJR Avatar asked Feb 14 '12 05:02

PJR


3 Answers

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
    }
}
like image 162
Srikar Appalaraju Avatar answered Sep 19 '22 19:09

Srikar Appalaraju


@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;
}
like image 37
rob mayoff Avatar answered Sep 20 '22 19:09

rob mayoff


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 {
       ...
    }
}
like image 5
Hari Kunwar Avatar answered Sep 19 '22 19:09

Hari Kunwar