我有一个显示为弹出窗口的视图控制器,我将其呈现在另一个视图控制器中。当我向左/向右滑动以关闭时,我想要这个视图控制器。我该怎么做?
我的视图控制器:
import UIKit
class DelayAlertViewController: UIViewController {
@IBOutlet var contentView: UIView!
@IBOutlet weak var viewShadow: UIView!
@IBOutlet weak var delayAlertView: DelayAlertView!
@IBOutlet weak var infoImage: UIImageView!
@IBOutlet weak var bannerLabel: UILabel!
private var message: String
init(with message: String) {
self.message = message
super.init(nibName: "DelayAlertViewController", bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .clear
self.modalPresentationStyle = .overCurrentContext
self.definesPresentationContext = false
self.delayAlertView.layer.borderWidth = 0.2
self.delayAlertView.layer.borderColor = UIColor.black.cgColor
self.viewShadow.addRoundedCornersShadow(radius: 1, width: 0, height: 0, opacity: 0.2)
delayAlertView.layer.masksToBounds = false
delayAlertView.layer.cornerRadius = 5
delayAlertView.clipsToBounds = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
}
如果我正确理解你的问题,请在 viewDidLoad 中添加以下内容:
override func viewDidLoad() {
super.viewDidLoad()
...
setupGestureRecognizers()
}
然后:
private func setupGestureRecognizers() {
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeAction(swipe:)))
leftSwipe.direction = UISwipeGestureRecognizer.Direction.left
view.addGestureRecognizer(leftSwipe)
}
最后是目标行动:
@objc func swipeAction(swipe: UISwipeGestureRecognizer) {
self.dismiss(animated: true)
}
更新: 如果您想要自定义关闭动画,例如向左或向右,请在 swipeAction 中尝试此代码:
@objc func swipeAction(swipe: UISwipeGestureRecognizer) {
let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromRight
self.view.window!.layer.add(transition, forKey: nil)
self.dismiss(animated: false, completion: nil)
}
有两种情况。
场景一:如果您的视图控制器位于 UINavigationController 内部,那么您可以使用苹果内置的 API,称为 InteractivePopGestureRecognizer。只需在 viewDidLoad 中添加以下行:
self.navigationController.interactivePopGestureRecognizer?.isEnabled = true
尽管添加此行后它不起作用,然后添加以下行:
self.navigationController.interactivePopGestureRecognizer?.delegate = nil;
场景二:如果您的视图控制器不在 UINavigationController 内,那么您可以在 viewDidLoad 中添加 UIPanGesture,如下所示:
首先删除名为 myPanGestureRecognizer 的实例,如下所示:
var myPanGestureRecognizer: UIPanGestureRecognizer!
然后在 viewDidLoad 中添加以下行:
myPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
myPanGestureRecognizer.delegate = self
self.view.addGestureRecognizer(myPanGestureRecognizer)
然后在 viewController 中的任意位置编写以下方法:
@objc func handlePanGesture(_ recognizer: UIPanGestureRecognizer) {
let velocity = recognizer.velocity(in: view)
if recognizer.state == .ended {
if velocity.x > 300 { // Adjust the value based on your requirement
self.dismiss(animated: true, completion: nil)
}
}
}