I currently have a view which contains a number of buttons and a single text field. The users can drag the buttons around, and when finished, interact with the text field. Dragging the buttons using UIPanGestureRecognizer seems to work, however once a user interacts with the text field, all of the buttons snap back to their original positions.
I would like it so that after dragging a button, the button's new position is saved, or at least doesn't revert back to its original position when interacting with other objects in the same view.
I have attached the code for my Gesture Recognizer:
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
//[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
UIButton *button = (UIButton*)recognizer.view;
if (recognizer.state == UIGestureRecognizerStateEnded)
{
button.center = recognizer.view.center;
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
}
Thank you for any help.
Make sure the buttons aren't handled with autolayout. If you are working in IB, make sure to remove all constraints from the buttons. Or, if you want to keep using autolayout, then update the constraints on the button rather than updating it's center position. You can do this by removing all old constraints and setting a new constraint positioning the button where you want it.
Short version
Remove constraints on the button if you want to move it