使用UIPanGestureRecognizer的wasDragged函数后,我试图编辑UILabel的文本。
我认为我可以在此功能中编辑输入标签的变量,也许是.reload,但似乎不起作用
var testLHS : String = "+ 2"
导入UIKit
AlgebraLearnViewController类:UIViewController {
var testLHS : String = "+ 2"
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel(frame: CGRect(x: 110, y: 167, width: 30, height: 30))
label.backgroundColor = UIColor.white
label.text = testLHS
label.textAlignment = NSTextAlignment.center
self.view.addSubview(label)
let gesture = UIPanGestureRecognizer(target: self, action: #selector(AlgebraLearnViewController.wasDragged(_:)))
label.addGestureRecognizer(gesture)
label.isUserInteractionEnabled = true
// Do any additional setup after loading the view.
}
@objc func wasDragged(_ gesture : UIPanGestureRecognizer){
let snapPosition = CGPoint(x: RHSconstantLabel.center.x + 20, y: RHSconstantLabel.center.y)
let translation = gesture.translation(in: self.view)
let label = gesture.view!
label.center = CGPoint(x: label.center.x + translation.x , y: label.center.y + translation.y)
gesture.setTranslation(CGPoint.zero, in: self.view)
if gesture.state == UIGestureRecognizerState.ended {
label.center = snapPosition
testLHS = "- 2"
}
}
任何想法表示赞赏。
谢谢