我有一个带有手势识别器的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)
}
}
但是,当我尝试在视图控制器中移动卡时没有任何反应。
非常感谢任何和所有帮助。