Ios 用手指触摸向左移动按钮,并在触摸结束时返回到其第一个位置
我是Swift新手,正在使用一个应用程序,在这个应用程序中,我移动绿色按钮(如图所示),用手指向左移动,当手指从按钮上移开时,按钮就会移动到以前的位置。还有四种不同的按钮背景图像,在每次按钮从当前位置向左移动后都会发生变化。但我无法理解我是如何做到这一点的。请帮我完成任务 这是我的密码Ios 用手指触摸向左移动按钮,并在触摸结束时返回到其第一个位置,ios,swift,uitouch,Ios,Swift,Uitouch,我是Swift新手,正在使用一个应用程序,在这个应用程序中,我移动绿色按钮(如图所示),用手指向左移动,当手指从按钮上移开时,按钮就会移动到以前的位置。还有四种不同的按钮背景图像,在每次按钮从当前位置向左移动后都会发生变化。但我无法理解我是如何做到这一点的。请帮我完成任务 这是我的密码 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { location = ((touc
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
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
位置=((touch.first)?.location(在:self.view中))!
让位置=视图。转换(位置,到:视图)
打印(“触摸开始\(位置)”)
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
位置=((touch.first)?.location(在:self.view中))!
让位置=视图。转换(位置,到:视图)
打印(“移动的触摸:\(位置)”)
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
//我不知道我的表现如何
}
//对于更改图像,请单击按钮
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;
}
}
我认为最好是制作一个gif动画,或者画两个有两个结束状态的图像。每个人都会明白你想做什么。用你的值替换50。var selected=true@IBAction func btnUserAction(u发件人:UIButton){if selected==true{sender.setImage(UIImage.init(名为:“tick”),for:.normal)selected=false;}else{sender.setImage(UIImage.init(名为:“unTick”),for:.normal)selected=true;}
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;
}
}