将向上滑动手势识别器添加到模态视图
Posted
技术标签:
【中文标题】将向上滑动手势识别器添加到模态视图【英文标题】:Add swipe-up gesture recognizer to a modal view 【发布时间】:2021-07-18 05:39:35 【问题描述】:我想向以模态方式呈现的视图添加向上滑动识别器。但是,似乎向上滑动和向下滑动手势是为模态呈现的视图中的视图关闭而保留的。当我将识别器设置为识别向左或向右滑动时,它工作得很好,但是当我将它设置为识别向上滑动时,它不起作用。我该如何解决这个问题?
【问题讨论】:
【参考方案1】:添加代理并覆盖手势:
class VC2: UIViewController
override func viewDidLoad()
super.viewDidLoad()
let direction: UISwipeGestureRecognizer.Direction = .up
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(swipeAction(sender:)))
gesture.direction = direction
gesture.delegate = self
self.view.addGestureRecognizer(gesture)
@objc func swipeAction(sender: UISwipeGestureRecognizer)
print(sender.direction)
extension VC2: UIGestureRecognizerDelegate
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool
return true
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool
return false
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool
return true
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
return true
【讨论】:
以上是关于将向上滑动手势识别器添加到模态视图的主要内容,如果未能解决你的问题,请参考以下文章