
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
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 Articles
- 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 if an iOS application is in background or foreground?
- How to detect which iOS version is running on the device?
- How to detect iOS device UDID, Name, Version, Model by programmatically?
- Detect Whether a Device is iOS or Not Using JavaScript
- Is a book printing press different from a magazine printing press?
- How to press a button without touching it on Tkinter?
- How to take a screenshot of my iOS application in the iOS simulator?
- How to press ENTER inside an edit box in Selenium?
- How to create a WebView in iOS/iPhone?
- Key press in (Ctrl+A) Selenium WebDriver.
- How to change Tkinter label text on button press?
