I have a UIButton that i’d like the user to be able to drag with TouchDragInside. How do i get the button to move as the user moves their finger?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As Jamie noted, a pan gesture recognizer is probably the way to go. The code would look something like what follows.
The button’s view controller might add a gesture recognizer to the button (possibly in
viewDidLoad) as follows:And, the view controller would have the following target method to handle the gesture:
CORRECTED as per rohan-patel’s comment.
In the previously posted code , the x and y coordinate’s of the origin of the button’s frame were set directly. It was incorrect as:
draggedButton.frame.origin.x += translation.x. A view’s frame can be changed, but the frame’s components cannot be changed directly.