How to create a button that spawns a movable image? iOS 5 - objective-c

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, call translationInView: on the gesture recogniser, passing in the view controller's view (i.e. the superview of the buttons). Now, if and only if shouldTrackButton is YES, take the frame of the original button, translate it by that amount (using CGRectApplyAffineTransform and CGAffineTransformMakeTranslation), and set the new button's frame to the result.
Finally, in the same method, if the gesture recogniser's state is UIGestureRecognizerStateEnded, set shouldTrackButton to NO.

Related

UIButton steals UIView touch events

I have a class which subclassed UIView. The class only have touch methods (to rotate view according to users touch).
From storyboard i added a view to my ViewController's view and subclassed it with that custom class. The idea behind to receive its touch events, thats it.
In that View some UIButton is place covering the whole view.
Now requirement is when user clicks on button then its action should call else if he tries to rotate the view it should give spin rotation.
Every thing is working fine if i make user interaction of button to be false as obviously now Uiview is the responder. But this not what i want. I want button to be interact to action as usual and if user move his figure then rotate the view.
I achieve same thing with UIRotationGestureRecognizer but it is meant with 2 figures touch and i need to achieve same thing with single finger touch.
Any help and suggestions will be appreciated.

3 programmatically created UIButtons - how to go between 3 UIViewControllers using those buttons with Storyboard?

Playing around with some Objective-C (well, iOS) and have done the following... loaded some UIButtons programmatically into a UIScrollView. That works well. Though I've always just connected UIViewControllers together using control-click and drag. Now I've created buttons programmatically, I have no idea how to go from one view controller to another in a Storyboard because there is nothing to drag from!
I'm not really sure what code to post as such, because I haven't done anything that /nearly/ works or doesn't work as such. I get how to do it with XIBs. But I suppose the question is : 3 UIButtons have been created programmatically and I have 3 UIViewControllers. How do I access those ViewControllers using my UIButtons?
Thanks
In the Interface builder view control click and drag from the viewcontroller icon under the first view controller, to the middle of the second view controller. A segue will be created, selected the appropriate type.
Now select the segue and in the inspector give it a unique identifier (say 'myNewSegue').
Now in your first viewcontroller you can create a method that has the following code:
-(void)myButtonAction:(id)sender {
[self performSegueWithIdentifier:#"myNewSegue" sender:self];
}
And add this method as a target action to your button:
[myButton addTarget:self
action:#selector(myButtonAction:)
forControlEvents:UIControlEventTouchUpInside];]
A segue doesn't have to have a button at the leading end of it; you can instead draw it from an entire view controller to another. You can also give a segue an identifier, a string that's used as a name for that segue. Once you've done that, you can programmatically trigger that segue by calling -performSegueWithIdentifier:sender:.
To actually call -performSegueWithIdentifier:sender:, though, you'll need to connect the button to a target and action. If you've never done that, read the Event Handling Guide for iOS in your documentation.

How to swap views, using UIGestureRecognizers

I have set the left back button item of a navigation controller to a custom view and have set up a tap gesture recognizer for it, now when the custom view receives a tap gesture it pops the current view controllers vie of a stack of navigation controllers.
I want to now switch the view when the back button is tapped, and still maintain the functionality similar to a button, like I long press it and it will only pop the current view controller once I have let go.
You can add a UILongPressGestureRecognizer to your custom view in addition to the tap recognizer.
Note that UILongPressGestureRecognizer is a continuous recognizer: one long-press will generate several action messages. You need to check for state UIGestureRecognizerStateEnded in your handler.

Why isn't my navigation bar controller handling the touchesEnded event on my navigation bar view?

This question came closest to describing my problem, but I'm missing something in the general process and the answer eventually goes into "Never mind, I figured out a different solution that isn't described".
In my case, I've got an XIB with a navigation bar and its controller. For the class fields, I've filled in my custom class names. Here's a screenshot showing the XIB, because I think this should be a relatively simple and straightforward setup...
What I want (like in link at the beginning) is for the touchesEnded event to fire so that I can do something. This works for the "view"; I can programmatically write in the event, set a breakpoint, and see that breakpoint get hit. However, my higher-level goal is to push a new view onto the app at that point -- something that I shouldn't do in the view's functionality, and which I can't do anyway because my view doesn't have access to the navigation controller (unless I do some trickery to retrieve the controller, but I want to do this cleanly).
Even though it works programmatically for the view, the touchesEnded event does not get hit for the navigation view's controller. I set it to the delegate, as shown in the image below, which I'm suspecting is perhaps only a part of what I need to do.
So now that I've set the delegate, and have seen that the touchesEnded event is being hit by the view, why isn't my navigation bar view controller picking up on the touchesEnded event? Am I mistaken and should instead be figuring out how to push a new view from the navigation bar's view, since I've seen that THAT touchesEnded event is being hit? It just seems like something I should be handling in the navigation bar view's controller, but I can't get that controller's touchesEnded event hit.
Thanks!
It's not clear from your post, but it looks like you're adding a navigation bar to your app explicitly, is that true? If your controller is embedded in a navigation controller, then there's no need to do that. You can add a view to the navigation bar, and add a UIImage view as a subview. If you set user interaction to be on, and add a tap gesture recognizer to the image view, you should be able to get the effect you want.
Then you have to override UINavigationBar, and override becomeFirstResponder, returning YES.

iOS perform action before handling touch events

I'm trying to recreate the Facebook slideout menu trick (yes I know there's lots of libraries already).
When a user touches a button on the screen, a new view controller is switched in and a rendered bitmap of the previous screen is shown in the foreground of this new view. The uiimageview has a pan gesture recogniser attached to it to allow it to be moved around on the screen.
Currently it's requiring two touches to activate the gesture recogniser, once to bring in the new view controller, and once for the pan gesture to move the uiimageview.
My question is, on touch down can I bring in the new view controller and then pass the touch events to the uiimageview? Or is there any other trick that will help?
I haven't managed to achieve this exactly, but instead I've subclassed UIViewController so the root view controller never changes, just the content that's being displayed.