移动按钮,返回到其第一位置,当触摸结束IOS斯威夫特
我是新来的斯威夫特和工作中,我将我的绿色按钮(显示在图片)的应用移动与手指到左侧,当手指从按钮中移除,然后按钮移动到其之前的位置。还有四个不同的按钮背景图像,在从当前位置到左侧按钮的每次移动之后都会改变。但我无法理解,我是如何做到这一点的。请帮我完成我的任务。移动按钮,返回到其第一位置,当触摸结束IOS斯威夫特
这里是我的代码
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;
}
}
用你的价值取代50。 –
我如何改变每个水龙头上的按钮图像?选择 – Irfan
VAR =真 @IBAction FUNC btnUserAction(_发件人:的UIButton){ 如果选择== TRUE { sender.setImage(UIImage.init(命名为: “嘀”),为:。中性) 选定=假; } 否则{ sender.setImage(UIImage.init(命名为: “取消选中”),为:。中性) 选定= TRUE; } } –
我觉得这是更好地做一个GIF动画或绘制两个具有两个结束状态的图像。比每个人都明白你想要做什么。 – blyabtroi