简体   繁体   中英

How to run code when view controller in iOS is dismissed by swiping down?

I have a view controller that I present in my iOS app using Swift and Xcode that is able to be dismissed by swiping down on it. I need to implement code that executes when the user swipes down on the view controller to dismiss it. I suspect there is a callback function that is called when that happens. Can anyone tell me if that's so and what the callback function is?

Without too much information this is how I would set it up:

My views would be on a Navigation controller

On the view I want to dismiss with down swipe I would use a Gesture recognizer:

override func viewDidLoad() {
   super.viewDidLoad()

   let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
        swipeDown.direction = .down
   view.addGestureRecognizer(swipeDown)
}

// Handle swipe down detected
@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    // Do whatever you want before you leave

    // Pop back a view
    if let nav = navigationController {
       nav.popViewController(animated: true)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM