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

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.

Related

UIButton Bug? (Xcode Version 4.6 (4H127))

1st: Xcode Version 4.6 (4H127), simulator 6.1
So... i do the following.
My button (inherited from UIButton) loaded from nib. Then i've set image to it for UIControlStateNormal.
I've got something like this:
After that i need to nullify it:
[button setImage:nil forState:UIControlStateNormal];
then i see this:
Check diffs:
So the title layouted right, but the image still there...
Also, if i do following:
[button setImage:[UIImage new] forState:UIControlStateNormal];
I get result like the second one, but without an image (as expected, but not fully, without layouting):
I am really interested in what is happening here?
Its really interesting, mb Apple broke something? Or i've just doing all wrong? But i've done it many times(setting image->nil), all worked Okay.
Thank you for reading :) I would appreciate for any help.
I have wrote a sample code regarding this.
Your both calls worked me fine
[self.myCustomButton setImage:nil forState:UIControlStateNormal];
[self.myCustomButton setImage:[UIImage new] forState:UIControlStateNormal];
I think, you may set the BackGroundImage instead of image.
Sample Methods
-(IBAction)removeImage:(id)sender{
[self.myCustomButton setImage:nil forState:UIControlStateNormal];
//[self.myCustomButton setImage:[UIImage new] forState:UIControlStateNormal];
}
-(IBAction)setImage:(id)sender{
[self.myCustomButton setImage:[UIImage imageNamed:#"Button_Disable.jpg"] forState:UIControlStateNormal];
}
This thing was here:
My subclass of UIButton overrides -layoutSubviews, where setting imageView.hidden using some criteria.
So, if hidden property was YES, when i've calling setImage:forState - this bug happened.
My fix was next:
I just start using alpha property instead of hidden. (I know that is dirty, but this is the fastest way i've found)

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 enable touchesMoved() with button action?

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.

Why is UIControlTouchUpInside UNDEFINED?

I've got a simple question but it has got me confounded. The code below gets an UNDEFINED error for UIControlTouchUpInside. What do I need to include or import? If I comment out the line, it works. So that means forState:UIControlStateNormal is defined. I'm new so hope you can help.
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Accept?" forState:UIControlStateNormal];
[button addTarget:self action:#selector(acceptTapped) forControlEvents:UIControlTouchUpInside];
You don't need to import anything. You need only consult the documentation, where you will find that the symbol you're looking for is UIControlEventTouchUpInside. You could also just type "UIControl" in XCode and hit escape. UIControlEventTouchUpInside will be in the list that pops up.

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.