我正在尝试以编程方式创建一个Draggable标签,我可以让Label出现但是一旦我尝试拖动它就会抛出错误。有什么建议?屏幕上绝对没有其他内容。它是一个空的View控制器。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let label = UILabel(frame: CGRectMake(self.view.bounds.width / 2 - 100 , self.view.bounds.height / 2 - 50, 200, 100))
label.text = "Drag Me!"
label.textAlignment = NSTextAlignment.Center
self.view.addSubview(label)
let gesture = UIPanGestureRecognizer(target: self, action: Selector("Was Dragged:"))
label.addGestureRecognizer(gesture)
label.userInteractionEnabled = true
}
答案 0 :(得分:0)
您不应在选择器名称中使用空格。你是否用选择器的名称创建了函数?
试试这个:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let label = UILabel(frame: CGRectMake(self.view.bounds.width / 2 - 100 , self.view.bounds.height / 2 - 50, 200, 100))
label.text = "Drag Me!"
label.textAlignment = NSTextAlignment.Center
self.view.addSubview(label)
let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged"))
label.addGestureRecognizer(gesture)
label.userInteractionEnabled = true
}
func wasDragged() {
print("i've been dragged")
}