我是Swift的新手并且正在处理一个应用程序,其中我移动我的绿色按钮(显示在图片中)用手指移动到左侧,当手指从按钮移开时,按钮移动到其先前的位置。此外,还有四个不同的按钮背景图像,每次按钮从当前位置移动到左侧后都会发生变化。但我无法理解我是如何做到这一点的。请帮我完成任务。
这是我的代码
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
location = ((touches.first)?.location(in: self.view))!
let position = view.convert(location, to: view)
print("Touches Began \(location)")
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
location = ((touches.first)?.location(in: self.view))!
let position = view.convert(location, to: view)
print("touches Moved: \(location)")
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// dont know how i perform
}
答案 0 :(得分:0)
@IBOutlet weak var yourButtonOutlet: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
yourButtonOutlet.addTarget(self, action: #selector(t1),for: .touchUpInside)
yourButtonOutlet.addTarget(self, action: #selector(t2),for: .touchDown)
// Do any additional setup after loading the view.
}
func t1() {
print("touch end")
yourButtonOutlet.frame = CGRect(x: yourButtonOutlet.frame.origin.x + 50, y: yourButtonOutlet.frame.origin.y, width: yourButtonOutlet.frame.size.width, height: yourButtonOutlet.frame.size.height)
}
func t2() {
print("touch Start")
yourButtonOutlet.frame = CGRect(x: yourButtonOutlet.frame.origin.x - 50, y: yourButtonOutlet.frame.origin.y, width: yourButtonOutlet.frame.size.width, height: yourButtonOutlet.frame.size.height)
}
//以及点击按钮上的更改图片
var selected = true
@IBAction func btnUserAction(_ sender: UIButton) {
if selected == true {
sender.setImage(UIImage.init(named: "tick"), for: .normal)
selected = false;
}
else{
sender.setImage(UIImage.init(named: "unTick"), for: .normal)
selected = true;
}
}