設定您所設定的框架後,新的值不會移動的按鈕變換身份因爲它只會返回新的幀。
或者:你需要存儲的舊框架中的變量,將其設置爲一個新值之前:
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)
}))
}
}
sender.transform = .identity不移回原來的位置? – Arrabidas92
沒有@ Arrabidas92 –