viewcontroller - 在iOS中,如何向下拖动以关闭模式,viewcontroller?

  显示原文与译文双语对照的内容
145 0

如果用户可以以拖动模态,如果已经经足够的话,我们将退出模态,否则将返回原始位置。

例如我们可以在app视图的Twitter 照片或者"snapchat发现"模式中找到它。

类似的线程指出,当用户触发时,我们可以以使用UISwipeGestureRecognizer和 [self dismissViewControllerAnimated...] 来消除模态的VC 。但这只处理一次滑动,而不允许用户拖动模式。

时间:原作者:0个回答

59 0

我将分享我在 Swift 3中的工作方式:

结果

实现
class MainViewController: UIViewController {
 @IBAction func click() {
 performSegue(withIdentifier:"showModalOne", sender: nil)
 }
}
class ModalOneViewController: ViewControllerPannable {
 override func viewDidLoad() {
 super.viewDidLoad()
 view.backgroundColor =. yellow
 }
 @IBAction func click() {
 performSegue(withIdentifier:"showModalTwo", sender: nil)
 }
}
class ModalTwoViewController: ViewControllerPannable {
 override func viewDidLoad() {
 super.viewDidLoad()
 view.backgroundColor =. green
 }
}

当模态视图控制器从 class 继承时,我已经经构建了( ViewControllerPannable ),使它们可以以在达到某个速度时。

ViewControllerPannable类
class ViewControllerPannable: UIViewController {
 var panGestureRecognizer: UIPanGestureRecognizer?
 var originalPosition: CGPoint?
 var currentPositionTouched: CGPoint?
 override func viewDidLoad() {
 super.viewDidLoad()
 panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panGestureAction(_:)))
 view.addGestureRecognizer(panGestureRecognizer!)
 }
 func panGestureAction(_ panGesture: UIPanGestureRecognizer) {
 let translation = panGesture.translation(in: view)
 if panGesture.state ==. began {
 originalPosition = view.center
 currentPositionTouched = panGesture.location(in: view)
 } else if panGesture.state ==. changed {
 view.frame.origin = CGPoint(
 x: translation.x,
 y: translation.y
 )
 } else if panGesture.state ==. ended {
 let velocity = panGesture.velocity(in: view)
 if velocity.y> = 1500 {
 UIView.animate(withDuration: 0.2
, animations: {
 self.view.frame.origin = CGPoint(
 x: self.view.frame.origin.x,
 y: self.view.frame.size.height
 )
 }, completion: { (isCompleted) in
 if isCompleted {
 self.dismiss(animated: false, completion: nil)
 }
 })
 } else {
 UIView.animate(withDuration: 0.2, animations: {
 self.view.center = self.originalPosition!
 })
 }
 }
 }
}
原作者:
141 3

为交互式拖动创建演示,以关闭视图控制器,如发现可运行模式。检查这里 github的示例项目。

enter image description here

原作者:
...