Combine longpress gesture and drag gesture togetherCombining a UILongPressGestureRecognizer with a UIPanGestureRecognizerHow to move UIView by longPress on button and then dragging?How to conditionally “pass through” a gesture from a UIButton to a UIScrollView?Executing pan gesture recognizer ONLY after long press recognizer has firedUnable to add two gesture to a UIViewUIButton inside a view that has a UITapGestureRecognizeriPhone: How to combine Long Press gesture and drag operation together on map?How to attach one UIPanGestureRecognizer object for several views?How can i detect pinch and tap in map view?how to stop a button from overruling a drag gestureNeed to rotate (fluring) image using Pan GestureRecognize long press and pan gesture recognizers togetheriOS Combining longPress and swipe gestureLong press and pan gesture in UICollectionViewiOS Objective C - Creating UIViews with LongPress / Using multiple gestures on same UIViews

How to hide an urban landmark?

Were Alexander the Great and Hephaestion lovers?

Importance of Building Credit Score?

is it possible for a vehicle to be manufactured witout a catalitic converter

What's up with this leaf?

Is it expected that a reader will skip parts of what you write?

Extreme flexible working hours: how to control people and activities?

Is an entry level DSLR going to shoot nice portrait pictures?

Fixing obscure 8080 emulator bug?

How to hide rifle during medieval town entrance inspection?

SQL counting distinct over partition

Is White controlling this game?

Winning Strategy for the Magician and his Apprentice

Check if three arrays contains the same element

Tabular make widths equal

Is using haveibeenpwned to validate password strength rational?

Does the Long March-11 increase its thrust after clearing the launch tower?

Overlapping String-Blocks

Has there been a multiethnic Star Trek character?

With Ubuntu 18.04, how can I have a hot corner that locks the computer?

Does a scale have more than seven chords?

Certain search in list

How to manually rewind film?

Which languages would be most useful in Europe at the end of the 19th century?



Combine longpress gesture and drag gesture together


Combining a UILongPressGestureRecognizer with a UIPanGestureRecognizerHow to move UIView by longPress on button and then dragging?How to conditionally “pass through” a gesture from a UIButton to a UIScrollView?Executing pan gesture recognizer ONLY after long press recognizer has firedUnable to add two gesture to a UIViewUIButton inside a view that has a UITapGestureRecognizeriPhone: How to combine Long Press gesture and drag operation together on map?How to attach one UIPanGestureRecognizer object for several views?How can i detect pinch and tap in map view?how to stop a button from overruling a drag gestureNeed to rotate (fluring) image using Pan GestureRecognize long press and pan gesture recognizers togetheriOS Combining longPress and swipe gestureLong press and pan gesture in UICollectionViewiOS Objective C - Creating UIViews with LongPress / Using multiple gestures on same UIViews






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








41















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?










share|improve this question






























    41















    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?










    share|improve this question


























      41












      41








      41


      15






      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?










      share|improve this question
















      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?







      iphone objective-c ios uigesturerecognizer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 9 '13 at 6:42









      Dilip

      7,77543454




      7,77543454










      asked Feb 14 '12 at 5:50









      PJRPJR

      9,711115795




      9,711115795






















          5 Answers
          5






          active

          oldest

          votes


















          83














          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







          share|improve this answer




















          • 3





            i am using CGPoint translatedPoint = [(UILongPressGestureRecognizer*)sender translationInView:self.view]; but tor long press what i have to use ?

            – PJR
            Feb 14 '12 at 6:23











          • try [yourView setFrame:CGRectMake(xCoord,yCoord,height,width)]

            – Srikar Appalaraju
            Feb 14 '12 at 6:26






          • 3





            but by this how can i get translatedPoint

            – PJR
            Feb 14 '12 at 6:30






          • 2





            no i want to move my view on long press and drag

            – PJR
            Feb 14 '12 at 7:04






          • 2





            @SrikarAppal you do not answer totally to the question. PJR wanted to know how to get the finger coordinates each time UIGestureRecognizerStateChanged was called. And the answer was: by using [myGestureRecognizer locationInView:aView]

            – Martin
            Jun 17 '16 at 16:55



















          36














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






          share|improve this answer























          • œrob: that`s the right answer! thank´s a lot!

            – rockstarberlin
            Dec 11 '12 at 21:44











          • what do you set priorPoint to initially?

            – RanLearns
            Apr 22 '18 at 17:57











          • You don't need to set it to anything in particular, because sender.state will always be Began before it is Changed. This means _priorPoint will always be initialized before it is used.

            – rob mayoff
            Apr 22 '18 at 18:06











          • But priorPoint starts off initially as (0.0, 0.0) so with this code the initial touch would move the center of the object by more than the difference of the point's location and the center's location. I had to set priorPoint to the view's center in the Began state

            – RanLearns
            Apr 22 '18 at 18:34











          • For future readers - I was hoping to combine the dragging movement of UIPanGestureRecognizer with the ability to take action on the initial tap using UITapGestureRecognizer. I played around with the answers here for UILongPressGestureRecognizer but while this seems to have worked for others, the calculation of the object's position as it is moved didn't reach the quality of using UIPanGestureRecognizer - which is what I ended up returning to using. For me this answer was helpful in providing a solution to recognize simultaneous gesture recognizers

            – RanLearns
            Apr 22 '18 at 19:35


















          3














          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
          ...







          share|improve this answer






























            2














            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)];






            share|improve this answer


















            • 3





              your solution only works for a UIView following the finger. Imagine you start the UILongPress from the corner of a picture and you want to swipe left, this picture would jump to the position of your finger. In my case, I had to use a _priorPoint solution.

              – Fabio
              Dec 10 '14 at 13:54











            • Very good point! Completely missed that, thank you!

              – Andrew
              May 5 '15 at 15:53


















            1














            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







            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9272333%2fcombine-longpress-gesture-and-drag-gesture-together%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              5 Answers
              5






              active

              oldest

              votes








              5 Answers
              5






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              83














              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







              share|improve this answer




















              • 3





                i am using CGPoint translatedPoint = [(UILongPressGestureRecognizer*)sender translationInView:self.view]; but tor long press what i have to use ?

                – PJR
                Feb 14 '12 at 6:23











              • try [yourView setFrame:CGRectMake(xCoord,yCoord,height,width)]

                – Srikar Appalaraju
                Feb 14 '12 at 6:26






              • 3





                but by this how can i get translatedPoint

                – PJR
                Feb 14 '12 at 6:30






              • 2





                no i want to move my view on long press and drag

                – PJR
                Feb 14 '12 at 7:04






              • 2





                @SrikarAppal you do not answer totally to the question. PJR wanted to know how to get the finger coordinates each time UIGestureRecognizerStateChanged was called. And the answer was: by using [myGestureRecognizer locationInView:aView]

                – Martin
                Jun 17 '16 at 16:55
















              83














              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







              share|improve this answer




















              • 3





                i am using CGPoint translatedPoint = [(UILongPressGestureRecognizer*)sender translationInView:self.view]; but tor long press what i have to use ?

                – PJR
                Feb 14 '12 at 6:23











              • try [yourView setFrame:CGRectMake(xCoord,yCoord,height,width)]

                – Srikar Appalaraju
                Feb 14 '12 at 6:26






              • 3





                but by this how can i get translatedPoint

                – PJR
                Feb 14 '12 at 6:30






              • 2





                no i want to move my view on long press and drag

                – PJR
                Feb 14 '12 at 7:04






              • 2





                @SrikarAppal you do not answer totally to the question. PJR wanted to know how to get the finger coordinates each time UIGestureRecognizerStateChanged was called. And the answer was: by using [myGestureRecognizer locationInView:aView]

                – Martin
                Jun 17 '16 at 16:55














              83












              83








              83







              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







              share|improve this answer















              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








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 24 at 18:06









              Cœur

              20.3k10119159




              20.3k10119159










              answered Feb 14 '12 at 6:00









              Srikar AppalarajuSrikar Appalaraju

              50.4k45186248




              50.4k45186248







              • 3





                i am using CGPoint translatedPoint = [(UILongPressGestureRecognizer*)sender translationInView:self.view]; but tor long press what i have to use ?

                – PJR
                Feb 14 '12 at 6:23











              • try [yourView setFrame:CGRectMake(xCoord,yCoord,height,width)]

                – Srikar Appalaraju
                Feb 14 '12 at 6:26






              • 3





                but by this how can i get translatedPoint

                – PJR
                Feb 14 '12 at 6:30






              • 2





                no i want to move my view on long press and drag

                – PJR
                Feb 14 '12 at 7:04






              • 2





                @SrikarAppal you do not answer totally to the question. PJR wanted to know how to get the finger coordinates each time UIGestureRecognizerStateChanged was called. And the answer was: by using [myGestureRecognizer locationInView:aView]

                – Martin
                Jun 17 '16 at 16:55













              • 3





                i am using CGPoint translatedPoint = [(UILongPressGestureRecognizer*)sender translationInView:self.view]; but tor long press what i have to use ?

                – PJR
                Feb 14 '12 at 6:23











              • try [yourView setFrame:CGRectMake(xCoord,yCoord,height,width)]

                – Srikar Appalaraju
                Feb 14 '12 at 6:26






              • 3





                but by this how can i get translatedPoint

                – PJR
                Feb 14 '12 at 6:30






              • 2





                no i want to move my view on long press and drag

                – PJR
                Feb 14 '12 at 7:04






              • 2





                @SrikarAppal you do not answer totally to the question. PJR wanted to know how to get the finger coordinates each time UIGestureRecognizerStateChanged was called. And the answer was: by using [myGestureRecognizer locationInView:aView]

                – Martin
                Jun 17 '16 at 16:55








              3




              3





              i am using CGPoint translatedPoint = [(UILongPressGestureRecognizer*)sender translationInView:self.view]; but tor long press what i have to use ?

              – PJR
              Feb 14 '12 at 6:23





              i am using CGPoint translatedPoint = [(UILongPressGestureRecognizer*)sender translationInView:self.view]; but tor long press what i have to use ?

              – PJR
              Feb 14 '12 at 6:23













              try [yourView setFrame:CGRectMake(xCoord,yCoord,height,width)]

              – Srikar Appalaraju
              Feb 14 '12 at 6:26





              try [yourView setFrame:CGRectMake(xCoord,yCoord,height,width)]

              – Srikar Appalaraju
              Feb 14 '12 at 6:26




              3




              3





              but by this how can i get translatedPoint

              – PJR
              Feb 14 '12 at 6:30





              but by this how can i get translatedPoint

              – PJR
              Feb 14 '12 at 6:30




              2




              2





              no i want to move my view on long press and drag

              – PJR
              Feb 14 '12 at 7:04





              no i want to move my view on long press and drag

              – PJR
              Feb 14 '12 at 7:04




              2




              2





              @SrikarAppal you do not answer totally to the question. PJR wanted to know how to get the finger coordinates each time UIGestureRecognizerStateChanged was called. And the answer was: by using [myGestureRecognizer locationInView:aView]

              – Martin
              Jun 17 '16 at 16:55






              @SrikarAppal you do not answer totally to the question. PJR wanted to know how to get the finger coordinates each time UIGestureRecognizerStateChanged was called. And the answer was: by using [myGestureRecognizer locationInView:aView]

              – Martin
              Jun 17 '16 at 16:55














              36














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






              share|improve this answer























              • œrob: that`s the right answer! thank´s a lot!

                – rockstarberlin
                Dec 11 '12 at 21:44











              • what do you set priorPoint to initially?

                – RanLearns
                Apr 22 '18 at 17:57











              • You don't need to set it to anything in particular, because sender.state will always be Began before it is Changed. This means _priorPoint will always be initialized before it is used.

                – rob mayoff
                Apr 22 '18 at 18:06











              • But priorPoint starts off initially as (0.0, 0.0) so with this code the initial touch would move the center of the object by more than the difference of the point's location and the center's location. I had to set priorPoint to the view's center in the Began state

                – RanLearns
                Apr 22 '18 at 18:34











              • For future readers - I was hoping to combine the dragging movement of UIPanGestureRecognizer with the ability to take action on the initial tap using UITapGestureRecognizer. I played around with the answers here for UILongPressGestureRecognizer but while this seems to have worked for others, the calculation of the object's position as it is moved didn't reach the quality of using UIPanGestureRecognizer - which is what I ended up returning to using. For me this answer was helpful in providing a solution to recognize simultaneous gesture recognizers

                – RanLearns
                Apr 22 '18 at 19:35















              36














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






              share|improve this answer























              • œrob: that`s the right answer! thank´s a lot!

                – rockstarberlin
                Dec 11 '12 at 21:44











              • what do you set priorPoint to initially?

                – RanLearns
                Apr 22 '18 at 17:57











              • You don't need to set it to anything in particular, because sender.state will always be Began before it is Changed. This means _priorPoint will always be initialized before it is used.

                – rob mayoff
                Apr 22 '18 at 18:06











              • But priorPoint starts off initially as (0.0, 0.0) so with this code the initial touch would move the center of the object by more than the difference of the point's location and the center's location. I had to set priorPoint to the view's center in the Began state

                – RanLearns
                Apr 22 '18 at 18:34











              • For future readers - I was hoping to combine the dragging movement of UIPanGestureRecognizer with the ability to take action on the initial tap using UITapGestureRecognizer. I played around with the answers here for UILongPressGestureRecognizer but while this seems to have worked for others, the calculation of the object's position as it is moved didn't reach the quality of using UIPanGestureRecognizer - which is what I ended up returning to using. For me this answer was helpful in providing a solution to recognize simultaneous gesture recognizers

                – RanLearns
                Apr 22 '18 at 19:35













              36












              36








              36







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






              share|improve this answer













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







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Feb 14 '12 at 9:12









              rob mayoffrob mayoff

              300k43602653




              300k43602653












              • œrob: that`s the right answer! thank´s a lot!

                – rockstarberlin
                Dec 11 '12 at 21:44











              • what do you set priorPoint to initially?

                – RanLearns
                Apr 22 '18 at 17:57











              • You don't need to set it to anything in particular, because sender.state will always be Began before it is Changed. This means _priorPoint will always be initialized before it is used.

                – rob mayoff
                Apr 22 '18 at 18:06











              • But priorPoint starts off initially as (0.0, 0.0) so with this code the initial touch would move the center of the object by more than the difference of the point's location and the center's location. I had to set priorPoint to the view's center in the Began state

                – RanLearns
                Apr 22 '18 at 18:34











              • For future readers - I was hoping to combine the dragging movement of UIPanGestureRecognizer with the ability to take action on the initial tap using UITapGestureRecognizer. I played around with the answers here for UILongPressGestureRecognizer but while this seems to have worked for others, the calculation of the object's position as it is moved didn't reach the quality of using UIPanGestureRecognizer - which is what I ended up returning to using. For me this answer was helpful in providing a solution to recognize simultaneous gesture recognizers

                – RanLearns
                Apr 22 '18 at 19:35

















              • œrob: that`s the right answer! thank´s a lot!

                – rockstarberlin
                Dec 11 '12 at 21:44











              • what do you set priorPoint to initially?

                – RanLearns
                Apr 22 '18 at 17:57











              • You don't need to set it to anything in particular, because sender.state will always be Began before it is Changed. This means _priorPoint will always be initialized before it is used.

                – rob mayoff
                Apr 22 '18 at 18:06











              • But priorPoint starts off initially as (0.0, 0.0) so with this code the initial touch would move the center of the object by more than the difference of the point's location and the center's location. I had to set priorPoint to the view's center in the Began state

                – RanLearns
                Apr 22 '18 at 18:34











              • For future readers - I was hoping to combine the dragging movement of UIPanGestureRecognizer with the ability to take action on the initial tap using UITapGestureRecognizer. I played around with the answers here for UILongPressGestureRecognizer but while this seems to have worked for others, the calculation of the object's position as it is moved didn't reach the quality of using UIPanGestureRecognizer - which is what I ended up returning to using. For me this answer was helpful in providing a solution to recognize simultaneous gesture recognizers

                – RanLearns
                Apr 22 '18 at 19:35
















              œrob: that`s the right answer! thank´s a lot!

              – rockstarberlin
              Dec 11 '12 at 21:44





              œrob: that`s the right answer! thank´s a lot!

              – rockstarberlin
              Dec 11 '12 at 21:44













              what do you set priorPoint to initially?

              – RanLearns
              Apr 22 '18 at 17:57





              what do you set priorPoint to initially?

              – RanLearns
              Apr 22 '18 at 17:57













              You don't need to set it to anything in particular, because sender.state will always be Began before it is Changed. This means _priorPoint will always be initialized before it is used.

              – rob mayoff
              Apr 22 '18 at 18:06





              You don't need to set it to anything in particular, because sender.state will always be Began before it is Changed. This means _priorPoint will always be initialized before it is used.

              – rob mayoff
              Apr 22 '18 at 18:06













              But priorPoint starts off initially as (0.0, 0.0) so with this code the initial touch would move the center of the object by more than the difference of the point's location and the center's location. I had to set priorPoint to the view's center in the Began state

              – RanLearns
              Apr 22 '18 at 18:34





              But priorPoint starts off initially as (0.0, 0.0) so with this code the initial touch would move the center of the object by more than the difference of the point's location and the center's location. I had to set priorPoint to the view's center in the Began state

              – RanLearns
              Apr 22 '18 at 18:34













              For future readers - I was hoping to combine the dragging movement of UIPanGestureRecognizer with the ability to take action on the initial tap using UITapGestureRecognizer. I played around with the answers here for UILongPressGestureRecognizer but while this seems to have worked for others, the calculation of the object's position as it is moved didn't reach the quality of using UIPanGestureRecognizer - which is what I ended up returning to using. For me this answer was helpful in providing a solution to recognize simultaneous gesture recognizers

              – RanLearns
              Apr 22 '18 at 19:35





              For future readers - I was hoping to combine the dragging movement of UIPanGestureRecognizer with the ability to take action on the initial tap using UITapGestureRecognizer. I played around with the answers here for UILongPressGestureRecognizer but while this seems to have worked for others, the calculation of the object's position as it is moved didn't reach the quality of using UIPanGestureRecognizer - which is what I ended up returning to using. For me this answer was helpful in providing a solution to recognize simultaneous gesture recognizers

              – RanLearns
              Apr 22 '18 at 19:35











              3














              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
              ...







              share|improve this answer



























                3














                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
                ...







                share|improve this answer

























                  3












                  3








                  3







                  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
                  ...







                  share|improve this answer













                  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
                  ...








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 31 '17 at 21:13









                  Hari KunwarHari Kunwar

                  982810




                  982810





















                      2














                      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)];






                      share|improve this answer


















                      • 3





                        your solution only works for a UIView following the finger. Imagine you start the UILongPress from the corner of a picture and you want to swipe left, this picture would jump to the position of your finger. In my case, I had to use a _priorPoint solution.

                        – Fabio
                        Dec 10 '14 at 13:54











                      • Very good point! Completely missed that, thank you!

                        – Andrew
                        May 5 '15 at 15:53















                      2














                      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)];






                      share|improve this answer


















                      • 3





                        your solution only works for a UIView following the finger. Imagine you start the UILongPress from the corner of a picture and you want to swipe left, this picture would jump to the position of your finger. In my case, I had to use a _priorPoint solution.

                        – Fabio
                        Dec 10 '14 at 13:54











                      • Very good point! Completely missed that, thank you!

                        – Andrew
                        May 5 '15 at 15:53













                      2












                      2








                      2







                      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)];






                      share|improve this answer













                      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)];







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 15 '14 at 19:16









                      AndrewAndrew

                      7061817




                      7061817







                      • 3





                        your solution only works for a UIView following the finger. Imagine you start the UILongPress from the corner of a picture and you want to swipe left, this picture would jump to the position of your finger. In my case, I had to use a _priorPoint solution.

                        – Fabio
                        Dec 10 '14 at 13:54











                      • Very good point! Completely missed that, thank you!

                        – Andrew
                        May 5 '15 at 15:53












                      • 3





                        your solution only works for a UIView following the finger. Imagine you start the UILongPress from the corner of a picture and you want to swipe left, this picture would jump to the position of your finger. In my case, I had to use a _priorPoint solution.

                        – Fabio
                        Dec 10 '14 at 13:54











                      • Very good point! Completely missed that, thank you!

                        – Andrew
                        May 5 '15 at 15:53







                      3




                      3





                      your solution only works for a UIView following the finger. Imagine you start the UILongPress from the corner of a picture and you want to swipe left, this picture would jump to the position of your finger. In my case, I had to use a _priorPoint solution.

                      – Fabio
                      Dec 10 '14 at 13:54





                      your solution only works for a UIView following the finger. Imagine you start the UILongPress from the corner of a picture and you want to swipe left, this picture would jump to the position of your finger. In my case, I had to use a _priorPoint solution.

                      – Fabio
                      Dec 10 '14 at 13:54













                      Very good point! Completely missed that, thank you!

                      – Andrew
                      May 5 '15 at 15:53





                      Very good point! Completely missed that, thank you!

                      – Andrew
                      May 5 '15 at 15:53











                      1














                      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







                      share|improve this answer





























                        1














                        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







                        share|improve this answer



























                          1












                          1








                          1







                          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







                          share|improve this answer















                          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








                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 22 '18 at 23:45









                          HAVB

                          1,10011223




                          1,10011223










                          answered Mar 22 '18 at 22:45









                          Lubomir.OLubomir.O

                          112




                          112



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9272333%2fcombine-longpress-gesture-and-drag-gesture-together%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                              Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript