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;
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
add a comment |
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
add a comment |
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
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
iphone objective-c ios uigesturerecognizer
edited Aug 9 '13 at 6:42
Dilip
7,77543454
7,77543454
asked Feb 14 '12 at 5:50
PJRPJR
9,711115795
9,711115795
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
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 UILongPressGestureRecognizer
selector 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
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 timeUIGestureRecognizerStateChanged
was called. And the answer was: by using[myGestureRecognizer locationInView:aView]
– Martin
Jun 17 '16 at 16:55
|
show 3 more comments
@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;
œ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, becausesender.state
will always beBegan
before it isChanged
. 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 theBegan
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
add a comment |
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
...
add a comment |
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)];
3
your solution only works for aUIView
following the finger. Imagine you start theUILongPress
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
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 UILongPressGestureRecognizer
selector 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
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 timeUIGestureRecognizerStateChanged
was called. And the answer was: by using[myGestureRecognizer locationInView:aView]
– Martin
Jun 17 '16 at 16:55
|
show 3 more comments
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 UILongPressGestureRecognizer
selector 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
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 timeUIGestureRecognizerStateChanged
was called. And the answer was: by using[myGestureRecognizer locationInView:aView]
– Martin
Jun 17 '16 at 16:55
|
show 3 more comments
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 UILongPressGestureRecognizer
selector 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
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 UILongPressGestureRecognizer
selector 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
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 timeUIGestureRecognizerStateChanged
was called. And the answer was: by using[myGestureRecognizer locationInView:aView]
– Martin
Jun 17 '16 at 16:55
|
show 3 more comments
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 timeUIGestureRecognizerStateChanged
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
|
show 3 more comments
@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;
œ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, becausesender.state
will always beBegan
before it isChanged
. 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 theBegan
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
add a comment |
@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;
œ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, becausesender.state
will always beBegan
before it isChanged
. 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 theBegan
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
add a comment |
@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;
@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;
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, becausesender.state
will always beBegan
before it isChanged
. 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 theBegan
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
add a comment |
œ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, becausesender.state
will always beBegan
before it isChanged
. 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 theBegan
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
add a comment |
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
...
add a comment |
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
...
add a comment |
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
...
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
...
answered Jan 31 '17 at 21:13
Hari KunwarHari Kunwar
982810
982810
add a comment |
add a comment |
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)];
3
your solution only works for aUIView
following the finger. Imagine you start theUILongPress
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
add a comment |
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)];
3
your solution only works for aUIView
following the finger. Imagine you start theUILongPress
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
add a comment |
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)];
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)];
answered Nov 15 '14 at 19:16
AndrewAndrew
7061817
7061817
3
your solution only works for aUIView
following the finger. Imagine you start theUILongPress
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
add a comment |
3
your solution only works for aUIView
following the finger. Imagine you start theUILongPress
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Mar 22 '18 at 23:45
HAVB
1,10011223
1,10011223
answered Mar 22 '18 at 22:45
Lubomir.OLubomir.O
112
112
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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