2017-09-24 121 views
0

我編程另一個函數創建14個按鈕和我添加的目標給他們,讓他們每個動畫一切,我已經目標時,我按任何按鈕去的目標,我的問題如何搬回原來的位置使用UIView.Animate將後退按鈕原來的位置UIView.Animate

正是這樣的Gif:

Demo

代碼:

var tileArrayXAxis: [Int] = [] // X-Axis of the my 14 buttons 
var tileArrayYAxis: [Int] = [] // Y-Axis of the my 14 buttons 
var tileArrayWidth: [Int] = [] // Width of the my 14 buttons 
var tileArrayHeight: [Int] = [] // Heigh of the my 14 buttons 

var targetArrayXAxis: [Int] = [] // X-Axis of the target 
var targetArrayYAxis: [Int] = [] // Y-Axis of the target 
var targetArrayWidth: [Int] = [] // Width of the target 
var targetArrayHeight: [Int] = [] // Heigh of the target 

var i : Int = 0 

func moveButton(sender: UIButton) { 

    var xS = Int() 
    var yS = Int() 
    var widthS = Int() 
    var heightS = Int() 

    if i < targetArrayXAxis.count && i < targetArrayYAxis.count && i < targetArrayWidth.count && i < targetArrayHeight.count { 

     xS = targetArrayXAxis[i] 
     yS = targetArrayYAxis[i] 
     widthS = targetArrayWidth[i] 
     heightS = targetArrayHeight[i] 
     yS -= widthS 

     let startS = CGPoint(x: xS, y: yS) 

     let sizeS = CGSize(width: widthS, height: heightS) 
     sender.frame = CGRect(x: startS.x, y: startS.y, width: sizeS.width, height: sizeS.width) 

    } 

    UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 6, options: .allowUserInteraction, animations: { [weak self] in 

     sender.transform = .identity 
     self?.i += 1   
    }) 
} 
+0

sender.transform = .identity不移回原來的位置? – Arrabidas92

+0

沒有@ Arrabidas92 –

回答

0

設定您所設定的框架後,新的值不會移動的按鈕變換身份因爲它只會返回新的幀。

或者:你需要存儲的舊框架中的變量,將其設置爲一個新值之前:

let oldFrame : CGRect = sender.frame 
// Now set the frame to its new value 
sender.frame = .... 

然後在你的動畫塊:

sender.frame = oldFrame 

或者:DO不分配一個新的框架,但發送者分配一個平移變換它,然後將轉換身份在動畫塊撤消轉換併發送按鈕回到它在哪裏。

編輯:按照要求,這種簡單的ViewController實現如下:

import UIKit 

類的ViewController:UIViewController的{

fileprivate var buttonOne : UIButton! 
fileprivate var buttonTwo : UIButton! 
fileprivate var buttonOneFrame : CGRect { 
    return CGRect(origin: CGPoint(x: view.bounds.width/2 - 200, y: 100), 
    size: CGSize(width: 150, height: 100)) 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    buttonOne = { 
     let bO : UIButton = UIButton() 
     bO.backgroundColor = .red 
     bO.setTitle("Frame Method", for: .normal) 
     bO.addTarget(self, action: #selector(frameMethodMove(sender:)), for: .touchUpInside) 
     bO.frame = buttonOneFrame 
     return bO 
    }() 

    buttonTwo = { 
     let bT : UIButton = UIButton() 
     bT.backgroundColor = .blue 
     bT.setTitle("Transform Method", for: .normal) 
     bT.addTarget(self, action: #selector(transformMethodMove(sender:)), for: .touchUpInside) 
     bT.frame = CGRect(origin: CGPoint(x: view.bounds.width/2 + 50, y: 100), 
          size: CGSize(width: 150, height: 100)) 
     return bT 
    }() 

    view.addSubview(buttonOne) 
    view.addSubview(buttonTwo) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

@objc fileprivate func frameMethodMove(sender: UIButton) -> Void { 

    let newFrame : CGRect = CGRect(origin: CGPoint(x: buttonOneFrame.origin.x, y: buttonOneFrame.origin.y + 500), 
            size: buttonOneFrame.size) 

    sender.frame = newFrame 

    sender.removeTarget(self, action: #selector(frameMethodMove(sender:)), for: .touchUpInside) 
    sender.addTarget(self, action: #selector(frameMethodMoveBack(sender:)), for: .touchUpInside) 
} 

@objc fileprivate func frameMethodMoveBack(sender: UIButton) -> Void { 

    UIView.animate(withDuration: 0.5, delay: 0, options: .allowUserInteraction, animations: { 
     sender.frame = self.buttonOneFrame 
    }, completion: ({ _ in 

     sender.removeTarget(self, action: #selector(self.frameMethodMoveBack(sender:)), for: .touchUpInside) 
     sender.addTarget(self, action: #selector(self.frameMethodMove(sender:)), for: .touchUpInside) 
    })) 

} 

@objc fileprivate func transformMethodMove(sender: UIButton) -> Void { 

    sender.transform = CGAffineTransform.init(translationX: 0, y: 500) 

    sender.removeTarget(self, action: #selector(transformMethodMove(sender:)), for: .touchUpInside) 
    sender.addTarget(self, action: #selector(transformMethodMoveBack(sender:)), for: .touchUpInside) 
} 

@objc fileprivate func transformMethodMoveBack(sender: UIButton) -> Void { 

    UIView.animate(withDuration: 0.5, delay: 0, options: .allowUserInteraction, animations: { 
     sender.transform = .identity 
    }, completion: ({ _ in 
     sender.removeTarget(self, action: #selector(self.transformMethodMoveBack(sender:)), for: .touchUpInside) 
     sender.addTarget(self, action: #selector(self.transformMethodMove(sender:)), for: .touchUpInside) 
    })) 
} 

}

+0

我想你解決,但是又回到了原來的位置自動我想回到原來的位置手動我的意思是按下它,我怎麼能這樣做? –

+0

請看看我的GIF,看看動畫4個按鈕目標和跳過位置並返回到原來的位置由按他們的@Sparky –

+0

@ user1.develop我明白你正在嘗試做的 - 用點擊返回按鈕是微不足道。你可以通過將這些方法拆分爲兩個並替換按鈕目標來實現。對於框架方法,您還需要將原始按鈕位置保存爲類屬性(無論是UIButton還是子類);在我看來,變換方法可能更優雅。請參閱我的編輯。 – Sparky