ios - Swift 4 - 识别 "Finger pan from view to view"

标签 ios swift xcode

所以我所拥有的是我有一个用于在 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/

相关文章:

ios - swift : "Mixing in"属性

swift - 将 Swift 属性绑定(bind)到 NSTableView?

ios - App更新后如何唤醒?

iphone - 使 UIPopoverController 在外部触摸时不辞职?

iOS6, shareSheet - iOS, xCode, Twitter, Facebook UIButton - 设计

ios - Xcode 和 Realm 浏览器的问题

ios - UITableView 滚动到最接近今天日期的部分

ios - 如何在导航栏中设置父 View 的文本字段全宽

ios - Swift 字典在追加新数据时删除以前的数据

IOS FoodTracker 教程 : Value of tuple type '()' has no member 'path'