我不明白为什么UILabel动作不起作用。 如果我在StoryBoard中创建UILabel,则UIPanGesture不会有问题。
func createLabel(textData: StoryText) {
let label = UILabel(frame: textData.frame)
let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePan(recognizer:)))
label.addGestureRecognizer(pan)
label.isUserInteractionEnabled = true
label.text = textData.text
label.font = textData.font
label.textColor = textData.color
self.photoImageView.addSubview(label)
}
@objc func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.photoImageView)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.photoImageView)
}
}