I am working on an iPhone app which requires me to check if the button has been tapped & held pressed for 6 seconds & then fire an action which is playing some sort of sound.
How should I detect this 6 second tap?
On the other hand the user can also keep on tapping button for 6 seconds & then the same action should fire.
What should I do with multiple taps, how would I know that all the taps fall under the 6 second bracket?
If you're an iPhone or iPad owner, then it's likely you know about the Long Press feature, also known as 3D touch or Press and Hold. This handy way of taking care of quick actions requires you to just touch and hold an app icon on your screen to pull up a list of actions you can perform.
With Android O's long-press feature, you can simply hold the app you want to use a widget for and tap the widget icon, which will give you all the available home screen buddies you've come to know and love.
A Long Press refers to pressing a physical button or tap a virtual button on a touchscreen and holding it down for a second or two. Employed on touchscreens, smartphones, tablets, and smartwatches, the long press or long tap increases the user interface's flexibility.
For a six second long press, use a UILongPressGestureRecognizer
with its minimumPressDuration
property set to 6.
Write your own gesture recognizer (say, LongTappingGestureRecognizer
) for continuous tapping for a given period; it shouldn't be too tricky. Give it a property like UILongPressGestureRecognizer
's minimumPressDuration
(say, minimumTappingDuration
) and a property (say, maximumLiftTime
) that determines how long a finger can be lifted off before it's not considered to be a long tapping gesture.
touchesBegan:withEvent:
, record the time.touchesEnded:withEvent:
, start an NSTimer
(the lift timer) that sends the gesture recognizer a cancel message (e.g. cancelRecognition
) after maximumLiftTime
. touchesBegan:withEvent:
when there's a start time, cancel the lift timer (if any). cancelRecognition
will transition to the failed state.There are various strategies for handling recognizing when the end of the gesture is reached, after minimumTappingDuration
. One is to check in both the touchesBegan:withEvent:
and touchesEnded:withEvent:
handlers if the difference between the current time and the start time is >= minimumTappingDuration
. The problem with this is that it will take longer than minimumTappingDuration
to recognize the gesture if the user is tapping slowly and hir finger is down when the minimumTappingDuration
is reached. Another approach is to start another NSTimer (the recognition timer) when the first touchesBegan:withEvent:
is received, one that will cause transition to the recognized state and that is cancelled in cancelRecognition
. The tricky thing here is what to do if the finger is lifted when the timer fires. The best approach might be a combination of the two, ignoring the recognition timer if the finger is lifted.
There's more to the details, but that's the gist. Basically, it's a long press recognizer that lets the user lift hir finger off the screen for brief periods. You could potentially use just the tapping recognizer and skip the long press recognizer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With