how to drag and drop UIButton - iphone

I want to drag and drop UIbutton in my app like iPhone main screen icons.
I can do this but while clicking the button it should perform an action. But this action is also called while dragging the button. I am using UIControlEventTouchDown for touch action and UIControlEventTouchDragInside for dragging the button.

Maybe you can use UIControlEventTouchUpInside for this action instead touch down.
In UIControlEventTouchDragInside you may change the shared variable named like "dragPerformed" to YES, and then, if it is NO perform click button action in UIControlEventTouchUpInside, if YES - do nothing (that is drag) and turn it to NO

Related

Swift - execute touch up inside if dragged

I have a UIButton which has a function for TouchUpInside. The problem I have is that when the user drags away from the button I would like to cancel the button from functioning, until of course the user presses the button again.
The function that's called when they drag away which is what I'm most likely going to use is TouchDragExit. Can anyone suggest anything in this case?
To elaborate more on what I mean, if the user is holding the button and decides to drag away then drag back to the button I don't want my touchUpInside function to function.
Register an action for the button's UIControl.Event.touchDragExit. When the button fires .touchDragExit, call the button's cancelTracking(with:), passing the UIEvent that it passed to your .touchDragExit action.

Slide to and from UIButtons and make them respond

I am creating a semi-piano app in Objective-C for iPhone; it's in fact a diffrent layout MIDI Controller, but I'm still working on the design.
I created all of the "keys" with UIButtons, and I want to be able to slide to them or from them to other UIButtons.
I read all the similar questions before, but I couldn't make it to work with touchesMoved,
Because it only worked from the view if I didn't drag the finger from a UIButton.
Thanks in advance, code will be appreciated!
Are your buttons created in IB? The default event when connecting buttons in IB is "touch up inside", which means the action is fired when the user ends a touch (lifts the finger) inside the button. A piano key or similar would be more appropriate to fire on the "touch down inside" (for the initial press) and the "touch drag enter" (for when a finger that is already on the screen moves into the button).
Since you are doing it programmatically, you can add targets to the buttons as described here:
http://developer.apple.com/library/ios/documentation/uikit/reference/UIControl_Class/Reference/Reference.html#//apple_ref/occ/instm/UIControl/addTarget:action:forControlEvents:
Just include the events you want in the mask.

Is it possible to click and drag the title from one UIButton and drop it into the title of another UIButton?

I have two UIButton's and I want to be able to have the user click one of the UIButton's and drag it over to another UIButton and have the title of the UIButton moved from the first UIButton to the second one. Is that possible? Thanks.
The title is an element of UIButton, so you can't use any predefined methods to do this. You'd have to create custom implementation where you added a draggable UILabel on touch, then set the button's title to match on release - or something like that.
I have created another answer that will at least get you on your way, it implements the press, the drag, and the up events for a button, where you are trying to recognize another button that you have dragged to!!!
Xcode iOS push down button and drag then up on a second button
Look at the answer provided by me!

How to open a view with a button clicked as default

In my app i want to open a view with the content of a particular button (so that button should look clicked and should be not clickable). I have 4 button with pictures and all the four have different content inside them (Table view with different content).When this view gets open i want the first button clicked automatically and the content of that button should get displayed and by clicking any other button the content of that button should get displayed and the previous clicked button should be available to click again.
I am using different pictures for clicked and unclicked button.
Thanks,
Maybe this will help you
- (void)didClickButton:(id)sender {
UIButton *optionButton = (UIButton *)sender;
if(lastSelectedButton.tag!= optionButton.tag) {
optionButton.selected = YES;
//According to your needs enable or disable the button's interaction
}
Here lastSelectedButton should be an instance variable.
What you're describing sounds like a segmented control. Essentially the segmented control works like buttons on a tape recorder (dating myself, I know.) When you press Play, it stays down and can't be pressed again until you press Stop or FF or Rew, etc. (Ok, Stop doesn't really work that way, but the rest of the buttons do)
Unfortunately, I don't believe you can use your own images in a UISegmentedControl, but fortunately there's an open-source version that should work for you: https://github.com/xhan/PlutoLand/blob/master/PLSegmentView.h
Once you have the control in place you can change the content of your main view depending on the value of the segmented control. You can handle that in the UIControlEventValueChanged event
Keep a single selector for all the buttons something like
[btn addTarget:self action:#selector(templateSelected:) forControlEvents:UIControlEventTouchUpInside];
and make use of the tag to carry any index to the selector
[btn setTag:integer];
and if you want to keep track of previously clicked button then keep a global (id) and assign the current button address to the that id.
And if you want the first button to be clicked on load then call the function melodramatically during initialization of the first button.
[self templateSelected:firstButton];

iPhone: programmatically press a button

Is it possible to press a button in iPhone SDK programmatically without the user touching it?
You can just do:
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
Do you want the user to see the button as pressed? Set the highlighted property to true and it will look like being pressed.
Now, if you just want to simulate the consequence of a button being pressed, just call the selector you assigned to the "Touch Up Inside" or "Touch Down Inside" events.
You could synthesize a touch on the button's coordinates, and set the appropriate event here's how
Overlay a second larger button that is transparent and assign the same selector it that you have on the other button.