How would you program a Continuous Delete Button? - ios

I'm making a calculator app and am supplying my own keypad with UIButtons. I have a delete key and everything works except that the user has to keep pressing the delete key over and over again if they want to delete all.
I was wondering if there is a way to delete everything when the button is held for more than 2 seconds.

The simplest way of implementing this would be attaching a long press gesture recognizer to your [Delete] button.
Xcode lets you attach long press gesture recognizer in the interface builder. Add it to your button, configure the duration of long press, and connect the handler to IBOutlet in the same way that you connect other UI events.
If you would rather do it in code, this answer shows you how.

Use your own timer function to handle this
-(IBAction)buttonHit {
//here start timer that fires for every 2 seconds and handle deletion method in that
}
-(IBAction)buttonReleased {
//Stop timer...
}

In your subclassed UIButton, you might want to watch the "touchesBegan: withEvent:" UIResponder method and if it passes a certain threshold of time, then start deleting like crazy (that is, until the "touchesEnded: withEvent" method gets called).

Related

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.

CoronaSDK- How to handle touch and tap at the same time

I have a case I couldn't find a solution for. In my game, moving your finger moves the objects around. But there is another scenario. If you are in a certain "mode", tapping certain object should do something specific, but if the user doesn't click on that specific object, I need to reset the mode to normal.
I have a system event touch handler that handles the move. I also have an event handler on the objects which are mode aware. Now the problem is resetting mode back to normal.
System touch event handler is called before object tap event, so I cannot handle it there as I am not sure if the tap event is going to fire or not. And if the tap didn't happen on the specific object, I have no way of handling it.
What to do?
I've handled a similar situation by just setting a Boolean to true when you enter the certain "mode" and setting it to false on every tap/touch end. - Throw a conditional in there to check if the current object is a recipient of the "mode", if not, set the dragging bool to false..
-Really nothing fancy needed from the SDK.
An object can have both a touch and a tap handler on it. A tap is a well defined short duration touch and release though and it may not do what you want, because if you touch, hold too long and then release, it won't register as a tap event.
Your best bet is to just code your touch handler to see if you got any move phases and if you get an end after a begin without any move's, then act as if you were tapped.

How can I post a manufactured event to UIButton

I'm writing a simple iOS 6.1 game. The game involves pressing buttons (OK, it's a tictactoe board, with the cells being UIButtons). The allows the player to choose whether to go first, or whether the computer should go first. If the player tells the computer to go first, I want to set some values, and then fire off the UIButton just as if the user had pressed it.
How can I post an event, or otherwise simulate the action of the button being pressed, to let the rest of the framework do the work of handling the button press?
If there is a better design approach, where I don't have to pretend that the computer has pressed a button, I'm open to hearing about that, instead.
Thank you
Your button will be connected to an action method, typically in your view controller. Just call that method yourself, passing the button as the sender.
Your method will be something like:
-(IBAction)buttonPressed:(UIButton*)sender
{
// Respond to your button press...
}
You'd call it as follows:
[self buttonPressed:self.whicheverButtonYouLike];
You'd need the buttons defined as outlets for this to work.

iOS: UIButton touch up inside doesn't respond multiple press

I have a UIButton whose IBAction has been setup though XIB file. Following is the IBAction method. When I press button multiple times very quickly, this method gets called only once for the last press. I have to wait little more time between the presses. Is there anything like long press time for UIButton that I can reduce or any other settings to make it responding quicker. This button does the similar job of Keyboard's back button to delete last character. I want it to respond very quick like Keyboard's button does. Thanks.
- (IBAction)deleteButtonPress:(id)sender {
NSLog(#"Click");
if(self.numpadTextFiled.text.length > 0)
self.numpadTextFiled.text = [self.numpadTextFiled.text substringToIndex: [self.numpadTextFiled.text length]-1];
}
Is the button by any chance positioned within a UIScrollView? If so, then touches are delayed?

UIButton TouchUpInside sometimes not firing

I am creating UIButtons programmatically and set their targets as well. Most of the time TouchDown-TouchDragInside-TouchUpInside chain seems to work properly but if I perform this chain of events fast (about 2-3 times per second) method bound to TouchUpInside is sometimes not firing.
From my understanding UIEvents will always fire even if they don't fire immediately. Is this a known issue that I can't seem to find anything about? What can I do about it, besides touching things slower?
I think you create button can switch and click.if so ,the two gesture will also do not handle well, you can add swipe gesture on the button , the it work well;Hope can help you.

Resources