【问题标题】:UIPanGestureRecognizer does not workUIPanGestureRecognizer 不起作用
【发布时间】:2016-04-16 08:49:19
【问题描述】:

这很奇怪,但是当我将 UIPanGestureRecognizer 添加到视图时它不起作用。这是我的观点,白色视图是红色视图的子视图:

主视图:

@IBOutlet weak var redView: UIView!
@IBOutlet weak var whiteView: UIView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let panRecognizer = UIPanGestureRecognizer(target:self, action:#selector(ViewController.handlePan))
    whiteView.gestureRecognizers = [panRecognizer]        
}


func handlePan(recognizer:UIPanGestureRecognizer)  {
    //foo
}

当我点击并拖动白色视图时,它不会移动。

【问题讨论】:

  • 这是一个 PangestureRecognizer,不是 TapgestureRecognizer???
  • 有什么问题?您已将手势识别器分配给白色视图。
  • @t0mm13b,我无法移动白色视图,这是问题所在。
  • 我的问题是我创建了一个UIGestureRecognizer 而不是UIPanGestureRecognizer

标签: swift uigesturerecognizer uipangesturerecognizer


【解决方案1】:

试试这个:

whiteView.userInteractionEnabled=true

【讨论】:

    【解决方案2】:

    来自 Apple 的文档:

    如果您的平移手势识别器的代码未被调用,请检查以下条件是否为真,并根据需要进行更正:

    视图的 isUserInteractionEnabled 属性设置为 true。默认情况下,图像视图和标签将此属性设置为 false。

    触摸次数介于 minimumNumberOfTouchesma​​ximumNumberOfTouches 属性中指定的值之间。

    对于 UIScreenEdgePanGestureRecognizer 对象,edges 属性已配置并且从适当的边缘开始触摸。

    Handling Pan Gestures

    【讨论】:

      【解决方案3】:

      答案如下:

      class ViewController: UIViewController {
      
          @IBOutlet weak var redView: UIView!
          @IBOutlet weak var whiteView: UIView!
      
          var lastLocation:CGPoint = CGPointMake(0, 0)
      
      
          override func viewDidLoad() {
              super.viewDidLoad()
              // Do any additional setup after loading the view, typically from a nib.
              let panRecognizer = UIPanGestureRecognizer(target:self, action:#selector(ViewController.handlePan))
              whiteView.addGestureRecognizer(panRecognizer)
      
          }
      
      
          func handlePan(recognizer:UIPanGestureRecognizer)  {
              let translation  = recognizer.translationInView(redView)
              whiteView.center = CGPointMake(lastLocation.x + translation.x, lastLocation.y + translation.y)
          }
      
          override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
              // Promote the touched view
              lastLocation = whiteView.center
      
          }
      }
      

      【讨论】:

        【解决方案4】:

        如果您使用 swift 语言作为方法,则不能使用 []

        var PanGesture = UIPanGestureRecognizer(target: self, action: "respondToPanGesture:")
        whiteView.addGestureRecognizer(PanGesture)
        

        希望对你有帮助。

        【讨论】:

        • 不是问题
        猜你喜欢
        • 2012-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多