当触摸结束 ios Swift 时,用手指触摸向左移动按钮 return 到它的第一个位置

Move Button left with finger touch and return to its first location when touch end ios Swift

我是 Swift 的新手,正在开发一个应用程序,在该应用程序中我将绿色按钮(如图所示)用手指移动到左侧,当手指从按钮上移开时,按钮将移动到它的前一个位置。还有四种不同的按钮背景图片,每次将按钮从当前位置移动到左侧后,这些图片都会发生变化。但我无法理解我是如何做到这一点的。请帮助我完成我的任务。

enter image description here

这是我的代码

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

}
    @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;
    }
}