UIPanGesture正在工作,但UITapGesture没有在UIView上工作

时间:2016-12-15 21:14:30

标签: ios swift uiview uigesturerecognizer uitapgesturerecognizer

我有一个小视图,我想在屏幕上移动并轻拍。所以我添加了一个UIPanGestureRecognizer来移动它,还有一个UITapGestureRecognizer来接收这样的点击事件:

        let panner = UIPanGestureRecognizer(target: self, action: #selector(panDidFire(panner:)))
        playerViewController.view.addGestureRecognizer(panner)

        let tapper = UITapGestureRecognizer(target: self, action: #selector(viewTapped(tapper:)))
        playerViewController.view.addGestureRecognizer(tapper)

我在同一个文件中创建动作

    func viewTapped(tapper: UITapGestureRecognizer){
         fadeInButtons()
    }
    func panDidFire(panner: UIPanGestureRecognizer) {
        let offset = panner.translation(in: view)
        panner.setTranslation(CGPoint.zero, in: view)
        var center = playerViewController.view.center
        center.x += offset.x
        center.y += offset.y
        playerViewController.view.center = center
    }

当用户平移时调用panDidFire()函数,但根本不调用viewTapped()函数。

这有诀窍吗?我究竟做错了什么?还有另一种方法吗?

我正在使用Xcode 8和swift 3

1 个答案:

答案 0 :(得分:1)

您必须设置识别器才能在视图上使用其他手势识别器。请使用UIGestureRecognizerDelegate的方法

gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
   return true
}