UIButton Long Press Event
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIButton does not have a dedicated built-in “long press” control event the way it has .touchUpInside. The standard UIKit solution is to attach a UILongPressGestureRecognizer to the button and react when the gesture begins.
Why a Gesture Recognizer Is the Right Tool
A long press is a gesture, not a normal button tap event. That means UIKit handles it through UIGestureRecognizer subclasses.
For a button, the usual setup is:
- create the button
- create a
UILongPressGestureRecognizer - attach the recognizer to the button
- handle the
.beganstate so your action runs only once per press
Basic Swift Example
The guard gesture.state == .began line is important. Without it, the handler may run multiple times as the gesture changes state.
Useful Gesture Properties
The most common configuration points are:
- '
minimumPressDuration: how long the finger must stay down' - '
allowableMovement: how much the finger may move before the gesture fails' - '
numberOfTouchesRequired: how many touches are needed'
Example:
This makes the gesture stricter and reduces accidental activation.
Tap and Long Press Together
A button can still respond to normal taps while also supporting a long press. Add the usual target-action for tap, then add the gesture recognizer separately.
That gives you two interaction paths:
- quick tap for the ordinary action
- long press for an alternate action such as preview, menu, or confirmation
This is often a better design than replacing the normal tap entirely. Users still get the standard button behavior, while power users can discover the extended long-press interaction when the UI makes that affordance clear.
Providing User Feedback
Long-press interactions feel better when the user gets immediate feedback. Haptics are a common choice.
You can trigger that inside the .began branch of the handler.
If the long press reveals a menu, preview, or destructive action, consider updating the button appearance too. A quick visual state change, such as tint or scale feedback, helps the user understand that the long press was recognized intentionally and not ignored.
Common Pitfalls
A common mistake is handling every gesture state instead of only .began. That makes the action fire more than once for a single press.
Another mistake is attaching the recognizer but forgetting that button tap handling and gesture handling can interact. Test both tap and long press so one does not unintentionally block the other.
A third issue is setting minimumPressDuration too low, which makes ordinary taps feel unreliable because the long press triggers too easily.
Summary
- '
UIButtonlong press behavior is implemented withUILongPressGestureRecognizer' - Attach the recognizer directly to the button
- Handle the
.beganstate so the action fires once per press - Adjust
minimumPressDurationandallowableMovementto tune the interaction - Use tap target-action and long press together when the button needs two behaviors
Related reading
- UIButton Making the hit area larger than the default hit area
- UIButton remove all target-actions
- UIButton title text color
- UIButton with two lines of text in the title numberOfLines2
- UIButton won't go to Aspect Fit in iPhone
- UICollectionReusableView method not being called
- UICollectionView - dynamic cell height?
- UICollectionView - Horizontal scroll, horizontal layout?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.