所以我所拥有的是我有一个用于在 View 上点击的UITapGestureRecognizer(我们称之为基本 View )和在同一基本 View 上的UILongPressGestureRecognizer。当用户点击 View 操作时,将调度一个 View 操作,如果用户按住 View ,则会弹出另一个 View 。这种行为效果很好。
我想要做的是用户不必单独点击弹出窗口。相反,我希望用户只需“拉起”弹出窗口并激活操作。
我尝试在弹出窗口上使用 UIPanGesture,但没有成功。
编辑
我将长按手势识别器拖到 View 上(实际上是自定义键盘中的一个键)。这是代码
@IBAction func popupPressed(_ sender: UITapGestureRecognizer) {
//Todo code for popupKey
}
@IBAction func keyPressedLong(_ sender: UILongPressGestureRecognizer) {
switch sender.state {
case .began:
popupTint = UIView(frame: self.frame)
popupTint!.backgroundColor = UIColor.black
popupTint!.alpha = 0.6
self.view.addSubview(popupTint!)
popup = UIView(frame: sender.view!.frame)
popup.frame = CGRect(x: popup.frame.origin.x, y: sender.view!.frame.origin.y - popup.frame.height, width: popup.frame.width, height: popup.frame.height)
popupKey.layer.cornerRadius = 5
popupKey.clipsToBounds = true
let popUpTap = UITapGestureRecognizer(target: self, action: #selector(self.popupPressed(tapGesture:)))
popupKey.addGestureRecognizer(popUpTap)
self.view.addSubview(popup)
default:
break
}
}
有什么想法吗?
提前非常感谢。
最佳答案
UILongPressGestureRecognizer 是连续的,因此您可以使用基本 View 中的位置来移动弹出窗口并检查是否满足移动阈值。
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.didLongPress(gesture:)))
self.baseView.addGestureRecognizer(longPress)
@objc func didLongPress(gesture: UILongPressGestureRecognizer) {
if self.popUpView.bounds.contains(gesture.location(in: self.popUpView)) {
// perform action
}
}
我希望这能回答您的问题。
关于ios - Swift 4 - 识别 "Finger pan from view to view",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51068505/