请在下面找到我在同一个用户界面中拖动2个元素的代码,但似乎不适用于2,当我开始使用一个元素时,一切正常。
当我添加第二个元素时,我在app delegate中出现此错误:
libc ++ abi.dylib:以未捕获的类型异常终止 NSException
你们有更好的练习吗?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel(frame:CGRectMake(self.view.bounds.width / 4 - 100, self.view.bounds.height / 2 - 50, 200, 100))
let label2 = UILabel(frame:CGRectMake(self.view.bounds.width / 1 - 200, self.view.bounds.height / 2 - 50, 200, 100))
label.text = "Drag me!"
label.textAlignment = NSTextAlignment.Center
self.view.addSubview(label)
label2.text = "drag me2!"
label2.textAlignment = NSTextAlignment.Center
self.view.addSubview(label2)
let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged"))
label.addGestureRecognizer(gesture)
label.userInteractionEnabled = true
let gesture2 = UIPanGestureRecognizer(target: self, action: ("wasDragged2"))
label2.addGestureRecognizer(gesture2)
label2.userInteractionEnabled = true
}
func wasDragged(gesture: UIPanGestureRecognizer) {
print("was dragged")
}
func wasDragged2(gesture2: UIPanGestureRecognizer) {
print("was draged2")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
您的操作选择器"wasDragged"
和"wasDragged2"
是错误的。那些不是你职能的名字。函数的名称是"wasDragged:"
和"wasDragged2:"
(注意每个字符串名称末尾的冒号)。