首页 文章

Swift 4 - 识别“手指平移从视图到视图”

提问于
浏览
0

所以 what I have 我有一个UITapGestureRecognizer用于视图上的Tap(让我们调用该基本视图)和同一个基本视图上的UILongPressGestureRecognizer . 当用户点击视图动作时,将调度一个,如果用户按住视图,则会弹出另一个视图 . 这种行为很好 .

What I want 做的是用户不必单独点击弹出窗口 . 相反,我希望用户只在弹出窗口"pull up"并激活一个动作 .

I tried to 在弹出窗口中使用UIPanGesture但是没有用 .

Edit

我将长按手势识别器拖到视图上(实际上是自定义键盘中的一个键) . 这是代码

@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
    }
}

有任何想法吗 ?

非常感谢提前 .

1 回答

  • 0

    UILongPressGestureRecognizer是连续的,因此您可以使用基本视图中的位置移动弹出窗口并检查是否满足移动阈值 .

    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
         }
    }
    

    我希望这回答了你的问题 .

相关问题