Right now I have a square button like so
OFF
ON
I want the button to change instantly when I first press on it (not wait for the finger to be lifted), so I need it to fire instantly and only once because I will also include a long press gesture for a different action. How would I go about making the button behave like this? Right now it only works for when I lift my finger off. (all the config has been done in the interface builder).
EDIT:
As liushuaikobe explained below I have set the event to "Touch Down" now. The button is now firing correctly but the image won't change until I take my finger off:
- (IBAction)pressButton:(id)sender {
NSLog(#"button pressed");
button.selected = YES;
}
The button should be selected instantly, I have the off image set in the interface builder for default and the on one set for selected. Any ideas what I'm doing wrong here?
Add a action for the button on state UIControlEventTouchDown.
This is the state when button is "pressed" rather than the finger left the screen (which is called UIControlEventTouchUpInside).
Connect your button action to your file owner with Touch Down event like this:
if your button connected with IBoutlet then you can do it by this way
[switchButton addTarget:self action:#selector(forgotBtnClick:) forControlEvents:UIControlEventTouchDown];
Set an image for the state highlighted:
[yourButton setImage: yourImage forState: UIControlStateHighlighted].
Set your highlighted image (UIButton's property) as on.png, assuming that default is off.png only if you want the effect not the method to be called, if you need your method to be called on press down then you have to change your control event from UIControlEventTouchUpInside to UIControlEventTouchDown
Related
I have a UIButton that is linked to an IBAction in a UIView. Within this IBAction, I both change the text of the UIButton's titleLabel and disable the UIButton. However, I've found that when the action fires, the button disables itself and changes its text for a split second before re-enabling itself. It's almost like it's happening just while the button is pressed, then it undoes itself. Could someone help with this? If it's useful, I'm on the latest Xcode and iOS 8 SDK.
//this is the IBAction...it happens, but the enabled property and text change quickly undoes itself.
#IBAction func solvePuzzle(sender: AnyObject) {
self.activity.startAnimating()
self.solveButton.enabled = false
self.solveButton.titleLabel?.text = "Solving..."
}
The text of the button should not be set like that. I am not very familiar with swift, but the right method should look something like:
self.solveButton.setTitle("Solving...", forState:UIControlState.Normal);
And also make sure that the method is called on the touchUpInside action.
http://youtu.be/TzPk4_hKozs
In this video, I made 5 button on view control.
When I touch button1 , it is highlighted.
I move my finger to button2 button 3 button4 button5...but there are no highlighted.
I wanna let the button highlighted when I move into their bounds.
How can I do?
please help me, thank you so much.
You won't be able to get that behavior with UIButton, but you can get it with not a lot of work.
Create a UIView subclass that can be set to be highlighted or not.
Add a UILongPressGestureRecognizer to the view displaying all 5 of the new views.
Specify an action and create a method to match, like -(void)longTouch:(UIGestureRecognizer *)gesture.
When the gesture state is UIGestureRecognizerStateBegan or UIGestureRecognizerStateChanged, determine what view your touch is over and highlight that view and unhighlight the rest.
I'd like to disable a button interaction if it is below another view.
On this example, my UIButton is the green frame.
I can tap on it and it will call the right selector (with control event "UIControlEventTouchUpInside"). But this call also work if I tap on the red frame which is above the green button, and I don't want that...
Any help to disable the interaction where the button isn't visible?
Fixed by setting :
theUpperView.userInteractionEnabled = YES;
I have a situation in which I need to have a button added to my view using Interface Builder, but the button shouldn't be seen, but if the user clicks the location of the button, the button can still call its method.
I've tried setting the opacity to zero, or changing the button to 'hidden', but both of these strategies result in a button that is completely invisible, but also useless, as it can't be pressed.
Does anyone have a solution for this?
You can try to custom type UIButton:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
With empty title, empty background image.
Or set the type from the storyboard or Interface Builder:
I am trying to make a UIButton be able to be enabled if I drag my finger from the region outside the button. It only works right now if I press the button like normal. I have tried using all of the events, but it doesn't look like it even will respond to any of the drag events. I have searched everywhere, but I cannot find an answer. Thanks!
You should need to use a selector to dragEnter event for a UIButton.
[your addTarget:self action:#selector(buttonDragged:)
forControlEvents:UIControlEventTouchDragEnter];
and implement the buttonDragged: method
-(void)buttonDragged:(UIButton*) sender{
//Your logic here
}