平移手势无法正常工作

Posted

技术标签:

【中文标题】平移手势无法正常工作【英文标题】:Pan gesture not working properly 【发布时间】:2018-04-03 07:08:19 【问题描述】:

所以我正在创建一个带有网格的应用程序,当我点击时会创建一个节点。现在,我目前正在使用 PanGesture 将节点拖放到网格中的不同位置。现在,我希望我拖动的节点更大(以便用户更容易看到),然后在它被放下时恢复到它的大小。所以我使用“panGesture.state == begin”来实现一个会放大节点的动画。但是,此动画仅在节点开始移动时才开始,而不是在我触摸它时才开始。根据 Apple API,

A pan gesture recognizer enters the began state as soon as the 
**required amount of initial movement is achieved**

意味着在我开始拖动节点之前,我的节点不会变大。

有什么办法可以解决这个问题吗?

【问题讨论】:

简单的解决方案是:将 UILongpressgesturerecognizer 也添加到您的视图中。所以当你扣除长按手势“放大节点”时。问题是:当您接到代表电话时,您必须区分这两种不同的手势。 @Mani 长按手势与平移手势完全相同,但会延迟触发。所以根本不需要2个识别器。所需要做的就是减少手势触发的最短持续时间并增加最大允许移动。 @MaticOblak 如何最小化手势需要触发的持续时间? 在答案中,您需要设置 minimumPressDuration。我不确定将其设置为零是否有效,但您可以尝试一些非常小的值。 【参考方案1】:

您可以同时使用长按和平移手势识别器。请参阅代码示例。

class ViewController: UIViewController, UIGestureRecognizerDelegate 
   var dragView: UIView!

   override func viewDidLoad() 
      super.viewDidLoad()

      self.dragView = UIView(frame: CGRect(x: 100.0, y: 100, width: 50.0, height: 50.0))
      self.dragView.backgroundColor = UIColor.red

      let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(pan))
      panRecognizer.delegate = self
      self.dragView.addGestureRecognizer(panRecognizer)

      let pressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(press))
      pressRecognizer.minimumPressDuration = 0.0 // IMPORTANT
      pressRecognizer.delegate = self
      self.dragView.addGestureRecognizer(pressRecognizer)

      self.view.addSubview(self.dragView)
   

   @objc
   func press(gestureRecognizer: UILongPressGestureRecognizer) 
      switch gestureRecognizer.state 
         case .began:
            UIView.animate(withDuration: 0.1, animations: 
               self.dragView.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
            )
         case .cancelled, .ended, .failed:
            UIView.animate(withDuration: 0.1, animations: 
               self.dragView.transform = CGAffineTransform.identity
            )
         default: break
      
   

   @objc
   func pan(gestureRecognizer: UIPanGestureRecognizer) 
      switch gestureRecognizer.state 
         case .changed:
            var center = self.dragView.center
            let translation = gestureRecognizer.translation(in: self.dragView)
            center.x += translation.x
            center.y += translation.y
            self.dragView.center = center
            gestureRecognizer.setTranslation(CGPoint.zero, in: self.dragView)
         default: break
      
   

   // MARK: UIGestureRecognizerDelegate
   @objc
   func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
                          shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool 
      return true // IMPORTANT
   

【讨论】:

【参考方案2】:

在平移手势中,.began 表示开始移动而不是开始触摸。您必须覆盖 func touchesBegan(_ touches: Set, with event: UIEvent?) 函数才能获得实际的触摸。

【讨论】:

以上是关于平移手势无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

带有平移、旋转和捏合手势的 UIKit Dynamics

捏合和点击手势无法正常工作

中心控制器具有滚动视图时的 MMDrawerController 平移手势行为

当从视图启用滚动时,SWRevealViewController 平移手势不起作用

创建自定义 UINavigationController - 使用 iOS 7 滑动手势无法正常工作

使用点击手势识别器时,IOS swift应用程序无法正常工作