以编程方式UIPanGestureRecognizer没有做任何事情

时间:2017-09-06 04:05:22

标签: ios swift uipangesturerecognizer

我有一个带有手势识别器的UIView,可以调用这样的函数:

let cardView: UIView = {
    let cv = UIView()

    cv.backgroundColor = .red
    cv.translatesAutoresizingMaskIntoConstraints = false
    cv.layer.cornerRadius = 5     
    cv.layer.masksToBounds = true     
    cv.isUserInteractionEnabled = true
    cv.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePan)))

    return cv
}()

和手势识别器的实现如下:

// pan functionality
func handlePan(gesture: UIPanGestureRecognizer) {
    let translation = gesture.translation(in: self.view)

    if let card = gesture.view {
        card.center = CGPoint(x: card.center.x + translation.x, y: card.center.y + translation.y)

    }

}

但是,当我尝试在视图控制器中移动卡时没有任何反应。

非常感谢任何和所有帮助。

0 个答案:

没有答案