My application has a UILongPressGestureRecognizer
, which display an alert with UIAlertController
when the user touches two elements on the screen. Currently, the user must press the OK button in order to dismiss the alert, however I would like to dismiss the alert after 0.5 seconds automatically, so the user doesn't compromise his interaction with the app.
Is there any way to do that?
You can dismiss the alert by calling dismissViewControllerAnimated method on alertController object.
Step 1 − Open Xcode and create a single view application and name it UIAlertSample. So basically, when we tap on the button an alert will be displayed, when the user taps outside the alert the alert will be dismissing.
You can achieve the automatic dismiss of alert view by using dispatch_after of GCD.
Try below code :
let delay = 0.5 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue()) { () -> Void in
self.dismissPopover()
}
Here, dismissCategoryPopover()
is a custom method that will be called automatically after 0.5 seconds to dismiss the alert view.
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