UIButton does not detect Touch Down right away - ios

I created UIButton through interface builder. That button has Touch Down event and Touch Up Inside event on it which triggers necessary code to be executed. Somehow the Touch Down IBAction linked to that button is not getting called right away I touch the button. I have to move my finger little bit before that action gets called. Did anyone face same kind of issue ?
Is it because I have two IBActions assigned to the same button ?
Thanks in advance.

Sounds like you linked your action to the wrong control event, specifically, it sounds like you linked the action to one of the Touch Drag... events.
Make sure that you hook your action to the button's Touch Down control event.

Related

Send user action to parent uiview in iOS

I am working on user interaction in different uiviews.
I know how to send user interaction on parent view or on it's specific UI components.
In my example, I am sending event to UIButton that is working properly even I tap on outside of UIButton bounds (Please take a look on attached code for more inside).
But I don't know that when I tapping on top view, UIButton TouchDown selector called but TouchUpInside not calling. Why is it happening?
Any suggestions? Any explanation is greatly appreciated!
Github code link: https://github.com/jackMac1811/iOSUIInteractionTest
All uibutton events carrying their own behavior which enables to execute its bunch of code according to it.
if you want to invoke both methods tapping on top view you should have to use touch up outside instead of touch up inside event
Here's a very useful link https://stackoverflow.com/a/11390048/4003548
Hope this helps ..

How do I toggle hidden of a label while a button is pressed?

I am trying to figure out how to only display a label while a button is pressed in OS. I know how to operate the touch events but I am not sure how to incorporate the UILongPressGestureRecognizer into this.
The UIButton class, as well as lots of other UIControl subclasses can have numerous actions hooked up to them.
When we are hooking up an action from interface builder to our source code file, if we open the "Event" drop down, we're presented with a long list of options:
In almost every scenario, we're hooking our actions only up to "Touch Up Inside". This allows the user to consider whether or not they want to really press the button. If they drag their finger off the button before letting go, the action doesn't fire, because the "up touch" gesture happened outside the bounds of the object.
But here, we want to actually hook our button's "touch down" event up. This is when we'll display the label.
Let's go ahead and create a "touch down" event and a "touch up inside" event:
Swift
#IBAction func buttonTouchDown(sender: UIButton) {
self.myLabel.hidden = false
}
#IBAction func buttonTouchEnded(sender: UIButton) {
self.myLabel.hidden = true
}
Objective-C
- (IBAction)buttonTouchDown:(UIButton *)sender {
self.myLabel.hidden = NO;
}
- (IBAction)buttonTouchEnded:(UIButton *)sender {
self.myLabel.hidden = YES;
}
So far, buttonTouchEnded is set up completely normally, and buttonTouchDown was set up by selecting "touch down" from the "Event" list.
We can always verify what our control is hooked up to by right clicking it in the interface builder:
But this menu is useful for more than simply checking what we've already hooked up. From here, we can hook up any of the other actions to our existing #IBAction methods simply by clicking in the circle and dragging to the existing method.
So we obviously want the label to disappear if we stop pressing the button, a normal touch up like you'd hook up any other button. The only question remaining is, what exact behavior do you want?
If you want the label to disappear only when the finger is lifted, no matter where the finger goes, then we must also hook up "touch up outside".
If you want the label to disappear when the user drags their finger off the button, then we should hook up the "touch drag exit" action.
We also probably want to hook up the "touch cancel" action, which would occur if some sort of system event (perhaps an incoming phone call) cancels the touch.
This Stack Overflow answer elaborates on the differences between the action options we have, so you can craft the behavior exactly how you need it.
Anyway, once we decide which actions we want to hook up to which methods, bring up that right click menu and click-drag from the circles to the methods:
The easiest thing to do would be to add an action to the touchDown event and a separate action to touchUpInside and touchUpOutside.
Show the label on the touchDown action and hide it on the touchUpInside / touchUpOutside action. (and for completeness, on touchCancel, as suggested by nhgrif in his very thorough answer.)
A long press gesture recognizer won't work in this situation. You could create a custom gesture recognizer that triggered one event on touch and another event on release, and use that. It's actually not that hard to do.
EDIT
I just uploaded a demo project to GitHub called "MorphingButton" (link) that I created for another question here on Stack Overflow.
That project now shows a label on touching the app button and hides the label when you release the button.
The project is a hybrid Swift/Objective-C project that shows how to do the button morphing and label showing/hiding in both languages. It has a tab bar with a Swift tab and an Objective-C tab.

Click on two buttons at the same time, if one covers the other one

What I want to do is just as the tiltle says. The reason is , I am not able to custome the view of the below button, so I plan to put another button on this button which can be customed by myself. And when I touch the upper one, the event of the belown one will be triggered.
Let's say there are two UIButtons A & B.
In the touch event (touchupinside) of A add the following code:
[buttonObjectB sendActionsForControlEvents: UIControlEventTouchUpInside];
This will trigger second button's touch event as you desire.

UIPanGestureRecognizer passes the event down to a button underneath

I have a side panel that has a UIImageView to which I attached a UIPanGestureRecognizer so that you could push/pull the sidebar.
It works well.
Thing is that I have some buttons that happen to sometimes occur underneath that sidebar. If I pull it and a button is underneath, the button would fire simultaneously with the Pan.
I am not sure what the problem is, so I find it hard to solve.
Why does my UIImageView pass the UIPanGestureRecognizer event on down the chain?
Problem solved with delegation.
I disable the events on toucheBegan if a BOOL called isPanning is set to YES for all buttons.
The protocol defines only one function:
-(void)setPanning:(BOOL)isPanning;
in the touchedBegan I check to see if the value is YES, if so I don't fire that event.
I wished it would be simpler.

UIButton's click action?

I am following a book to learn IOs programming. For UIButton, it supposed to have a click action. However I only saw some actions like touch down etc.
Also, want to know why the default action not the "touch down" but "touchUpInside"
I am using XCode 4.3.
click action in iOS is represented but touchUpInside, since click is a mouse event
touchUpInside, means that the user touched down on a button, and then touch up, while he still is on the same button, which is the normal behavior when you want to tap on a button
In storyboard, you can right click on the UIButton and see the list of events available for a UIButton and choose appropriate event based on your requirement.
For a click like action you can use Touch Up Inside event.

Resources