Reputation: 471
Intended function:
After long-pressing a button, have the ability to drag it around on the screen.
Jumping Bug:
When I initially start dragging the button, its center "jumps" to the CGPoint that registered my initial click to trigger the long press. E.g., I long-press click a button on its top right, and once I start dragging the cursor while holding it the button jumps to that "top right" location.
After that jump - all dragging is fine.
Code:
- (void)longPress:(UILongPressGestureRecognizer*)receivedGesture
{
if (receivedGesture.state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [receivedGesture locationInView:self.scrollView];
pannedBadge.center = translation;
}
}
Ultimate question:
What's the solution here? How do I make it such that the initial dragging moves the button from its original center?
Thanks!
Upvotes: 1
Views: 279
Reputation:
What I usually do for making views draggable is I detect the starting point of the touch, then get the difference with which it (the touch) moved, then set the view's center as follows: I don't set it to the place of the touch absolutely, but I set it relatively using the just detected movement of the touch.
Upvotes: 1
Reputation: 18363
The reason is that your translation point is asking the gesture's location in the view, not the center of the view it's over. If you start pannedBadge.center to be the button's center, I that should work. Hope this helps!
Upvotes: 0