简体   繁体   English

当通过向下滑动关闭 iOS 中的查看 controller 时如何运行代码?

[英]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 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.我需要实现当用户在视图 controller 上向下滑动以将其关闭时执行的代码。 I suspect there is a callback function that is called when that happens.我怀疑发生这种情况时会调用回调 function 。 Can anyone tell me if that's so and what the callback function is?谁能告诉我是不是这样以及回调 function 是什么?

Without too much information this is how I would set it up:如果没有太多信息,这就是我将如何设置它:

My views would be on a Navigation controller我的观点是导航 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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在iOS上关闭View Controller后,如何继续播放音频? - How to continue to play audio after view controller is dismissed on iOS? 向下滑动以关闭另一个视图 controller 时,有没有办法将数据传回视图 controller? - Is there a way to pass data back to a view controller when swiping down to dismiss another view controller? iOS GCD线程处理-视图控制器已关闭 - iOS GCD Thread Processing - View Controller Dismissed 模态视图控制器被关闭后如何调用函数 - How to call a function when a Modal View Controller has been dismissed 关闭时如何将数据从模态视图控制器传回 - How to pass data from modal view controller back when dismissed 关闭呈现的视图控制器时如何恢复焦点? - How to restore focus when presented view controller is dismissed? 当通过手势关闭呈现的视图 controller 时如何获得通知? - How to get notified when a presented view controller is dismissed with a gesture? iOS暂时禁用页面视图控制器刷卡 - iOS temporarily disable Page View Controller swiping 检测何时关闭呈现的视图控制器 - Detect when a presented view controller is dismissed 模态显示/关闭视图控制器时通知? - Notification when view controller is presented modally/dismissed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM