2017-05-25 35 views
0

我是新來的斯威夫特和工作中,我將我的綠色按鈕(顯示在圖片)的應用移動與手指到左側,當手指從按鈕中移除,然後按鈕移動到其之前的位置。還有四個不同的按鈕背景圖像,在從當前位置到左側按鈕的每次移動之後都會改變。但我無法理解,我是如何做到這一點的。請幫我完成我的任務。移動按鈕,返回到其第一位置,當觸摸結束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 

} 
+0

我覺得這是更好地做一個GIF動畫或繪製兩個具有兩個結束狀態的圖像。比每個人都明白你想要做什麼。 – blyabtroi

回答

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; 
    } 
} 
+0

用你的價值取代50。 –

+0

我如何改變每個水龍頭上的按鈕圖像?選擇 – Irfan

+0

VAR =真 @IBAction FUNC btnUserAction(_發件人:的UIButton){ 如果選擇== TRUE { sender.setImage(UIImage.init(命名爲: 「嘀」),爲:。中性) 選定=假; } 否則{ sender.setImage(UIImage.init(命名爲: 「取消選中」),爲:。中性) 選定= TRUE; } } –