[英]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.