

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to detect a long press in iOS?
Long-press (also known as press-and-hold) gestures detect one or more fingers touching the screen for an extended period of time. You configure the minimum duration required to recognize the press and the number of times the fingers must be touching the screen. (The gesture recognizer is triggered only by the duration of the touches and not by the force associated with them.) You might use a long-press gesture to initiate an action on the object being pressed. For example, you might use it to display a context-sensitive menu.
You can read more about it https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/handling_uikit_gestures/handling_long-press_gestures
Here we will be designing a simple application where we will press a button for certain period of time (Long press) and it will display an alert.
So let’s get started.
Step 1 − Open Xcode → New Projecr → Single View Application → Let’s name it “LongPressGesture”
Step 2 − In Main.storyboard add one button and create @IBOutlet of it and name it “btnLongOutlet”
Step 3 − Now open ViewController.swift and create an object of UILongPressGestureRecognizer()
var longgesture = UILongPressGestureRecognizer
Step 4 − In viewDidLoad() add the following code,
longgesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPress(_:))) longgesture.minimumPressDuration = 2 btnLongOutlet.addGestureRecognizer(longgesture)
Step 5 − Create a function longPress and add below code,
@objc func longPress(_ sender: UILongPressGestureRecognizer) { let alertController = UIAlertController(title: "Long Press", message: "Long Press Gesture Detected", preferredStyle: .alert) alertController.addAction(UIAlertAction(title: "OK", style: .default,handler: nil)) present(alertController, animated: true, completion: nil) }
Step 6 − And you’re done, Run the application, make sure you tap the button for 2 seconds.
- Related Questions & Answers
- How to detect long press in Android?
- Detect home button press in android
- How to detect user pressing HOME key in iOS?
- How to detect user inactivity for 5 seconds in iOS?
- How to detect which iOS version is running on the device?
- How to detect iOS device UDID, Name, Version, Model by programmatically?
- How to detect if an iOS application is in background or foreground?
- Is a book printing press different from a magazine printing press?
- How to press a button without touching it on Tkinter?
- Key press in (Ctrl+A) Selenium WebDriver.
- How to convert Long array list to long array in Java?
- How to take a screenshot of my iOS application in the iOS simulator?
- How to Detect Keyloggers?
- How to detect a new Android notification?
- How to create a WebView in iOS/iPhone?