I would like to create a button in my UIView that when touched spawns another button under the finger which immediately starts to follow the finger.
I have seen a tutorial here that deals with button following finger, but i can not figure out how to make this button follow finger WITHOUT user tapping it after creation.
Using iOS 5.1 SDK with XCode 4.3.2
Any help?
You can add target/action pairs to a button for various control events: to make something immediately start following your finger, you probably want
UIControlEventTouchDown.Hook up your first button to call a method on your view controller for that control state. In that action method, create your new button, and shove it in a property on your view controller so that you can refer to it later. Also set a BOOL property on your view controller that’s called something like
shouldTrackButton.To make the new button follow your finger, I guess the easiest way is a pan gesture recogniser on the superview. Set that up to call a method on your view controller. In that method, if the gesture recogniser’s state is
UIGestureRecognizerStateChanged, calltranslationInView:on the gesture recogniser, passing in the view controller’s view (i.e. the superview of the buttons). Now, if and only ifshouldTrackButtonisYES, take the frame of the original button, translate it by that amount (usingCGRectApplyAffineTransformandCGAffineTransformMakeTranslation), and set the new button’s frame to the result.Finally, in the same method, if the gesture recogniser’s state is
UIGestureRecognizerStateEnded, setshouldTrackButtontoNO.