2017-02-16 115 views
1

我正在使用CGAffineTransform來縮小點擊按鈕。按鈕寬度縮小,但從左側和右側會議在中間。但是,我希望按鈕從右側縮小。一個奇怪的比喻就像按鈕是一張紙,並且我點燃了右側的火柴,它燒燬了紙張,將它朝左側摧毀。我將如何實現這樣的功能?ios - 動畫按鈕從右邊開始縮小寬度

func animateStartButtonExit() { 


    UIView.animate(withDuration: 0.5, 
     animations: { 
      self.startButtonOutlet.transform = CGAffineTransform(scaleX: 0.1, y: 1) 

     }, 
     completion: { _ in 
       UIView.animate(withDuration: 0.5) { 
        self.startButtonOutlet.isHidden = true 
        self.startButtonOutlet.transform = CGAffineTransform.identity 
      } 
     }) 

} 
+0

嘗試基於幀的動畫,而不是'CGAffineTransform' – Mukesh

+0

或者你可以嘗試用約束動畫 –

回答

0

你可以試試這個是從動畫右側的按鈕,

UIView.animate(withDuration: 5, 
       animations: { 
       button.frame = CGRect(x: button.frame.minX, y: button.frame.minY, width: button.frame.size.width/2, height: button.frame.size.height/2) 

    }, 
       completion: { _ in 
       UIView.animate(withDuration: 0.5) { 
        button.isHidden = true 
        button.frame = CGRect(x: button.frame.minX, y: button.frame.minY, width: button.frame.size.width * 2, height: button.frame.size.height * 2) 
       } 
}) 
0

如果您正在使用自動版式,設定追蹤空間限制加起來,讓寬度0.1

rightConstraint.constant = rightConstraint.constant + width 

UIView.animate(withDuration: 5, 
       animations: { 
        button.layoutIfNeeded() 
    }, 
       completion: nil 
) 
1

它工作正常。我剛剛測試過。

enter image description here

class ViewController: UIViewController { 
    @IBOutlet weak var button: UIButton! 
    @IBOutlet weak var widthConstraint: NSLayoutConstraint! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    @IBAction func click(_ sender: UIButton) { 
     widthConstraint.constant = 10 
     UIView.animate(withDuration: 5, animations: { 
      self.view.layoutIfNeeded() 
     }, completion: { _ in 
      self.button.isHidden = true 
      // For example, back to the normal width 
      self.widthConstraint.constant = 168 
     }) 
    } 

} 

enter image description here