How to enable touchesMoved() with button action? - iphone

I am doing one piano application. There i am using button action to play music. But its not supporting drag functionalities. I have used touchesMoved(),touchBegan() methods also . But nothigh is working.Can anyone help me, please?

Button supports drag functionality. You should add your target by below control events.
[button addTarget:target action:#selector(action1) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:target action:#selector(action2) forControlEvents:UIControlEventTouchDragOutside];
[button addTarget:target action:#selector(action3) forControlEvents:UIControlEventTouchDragExit];
[button addTarget:target action:#selector(action4) forControlEvents:UIControlEventTouchDragEnter];
Implement proper actions for each event and it should identify proper drag event.

Related

iphone development: one button two actions

In my application I have a button and when it is pressed, lets say "touch Up inside" another view is opened. well my question is, how can I do two actions? I mean, when I pressing the button I want something to do(like hiding the button or changing the image of the button), and when I stop pressing I want to be navigated to the another view.
Do this:
[yourbutton addTarget:self action:#selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];
[yourbutton addTarget:self action:#selector(touchDown:) forControlEvents:UIControlEventTouchDown];
Now selectors are:
-(IBAction)touchUp :(id)sender{
UIButton *btn = (UIButton *)sender;
[btn setImage:yourImage forState:UIControlStateHighlighted];
}
-(IBAction)touchDown :(id)sender{
//Navigate here
}
If you want to 'do something' when 'pressing', then just hook up 'Touch Down'
The class that is the delegate for the button needs to track the state, and manage that state through the events that can occur (a state machine). Use instance variables to track the state.
When you handle the button action, the code should determine what state it is in, and make any changes necessary to move to the next state.
You can use different actions for different ControlEvents
[button addTarget:self action:#selector(action1) forControlEvents:UIControlEventTouchDown] ;
[button addTarget:self action:#selector(action2) forControlEvents:UIControlEventTouchUpInside] ;
Just add in your code to IBActions. Then like the actions through the interface builder to different touch events.
Code:
[yourUIButton addTarget:self action:#selector(touchUp:) forControlEvents:UIControlEventXXXXX];
There are a lot of events you can link your UIButton to:
UIControlEventTouchDown
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
UIControlEventTouchCancel
Ref:UIControl Apple Reference

How to remove the greyed out look of a disbled UIButton

I've got a UIButton that i want to look exactly the same when it's in its disabled state as when it's in its Normal state. Right now it has a slight greyed out look to it.
Do not use enabled property or setEnabled:NO method, instead use:
[myButton setUserInteractionEnabled:NO];
That would prevent the button for being touched, but without changing his looks!
The other way is if your button is a custom button and has an image:
[button setImage:someImage forState:UIControlStateNormal];
[button setImage:someImage forState:UIControlStateDisabled];
[button setEnabled:NO];

Create IBOutlet and connection with code

I am creating an iPad application that behaves like a power point presentational. I am creating several slides and each slides contains several images with basic functionality. Because I am creating so many slides it will be nice if I can create the IBOutles programmatically since I place so many buttons per slide for instance. it takes a while to place those outlets and create the connections. How can I speed up this process?
Why not just create every item programmatically?
Take a UIButton for example.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(40.0, 200.0, 170.0, 40.0);
[self.view addSubview:button];
This should be of interest to you: http://developer.apple.com/library/ios/#documentation/ToolsLanguages/Conceptual/Xcode4UserGuide/030-Edit_User_Interfaces/edit_user_interface.html#//apple_ref/doc/uid/TP40010215-CH6-SW1
There's even a nice video tutorial if you click: "To create and connect a new outlet ..."
Good luck!

How to determine which event called imageMoved:withEvent:forControlEvents?

I've added some targets to a button. They work fine. But in my imageMoved... method, how do I determine which one of the two events fired it? Here is where I added the targets:
[button addTarget:self action:#selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:#selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragExit];
I saw an example recently where the event is tested in the imageMoved method, something like:
if (event == UIControlEventTouchDragExit )
but that won't compile. I'm not finding any examples online nor any clues in UIControl Class Reference.
What I'm trying to do with this is store the original location of the button/image so that if the user doesn't complete the drag to a target it will snap back to where it came from.
Thanks for any help anyone can offer.
Use two different methods
[button addTarget:self action:#selector(imageMoved:withDragInsideEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:#selector(imageMoved:withDragExitEvent:) forControlEvents:UIControlEventTouchDragExit];
and reuse common functionality in 3rd method.

UIImageView as a link to a URL

How to make a UIImageView open a URL in Safari when user clicks it?
I would suggest using a button for this purpose instead. You can create a custom UIButton with whatever image you want. This gives the advantage of providing the built-in target-action mechanism of a button, as well as being able to provided a highlighted image to provide the user feedback. Consider using something like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"regular_image.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"highlighted_image.png"] forState:UIControlStateHighlighted];
[button addTarget:self action:#selector(loadURL) forControlEvents:UIControlEventTouchUpInside];
Note that it will still look like "just an image" to the user.