簡體   English   中英

如何從左到右和反向動畫UIButton框架

[英]How to animate UIButton frame from left to right and reverse

我當前的方法是向按鈕添加一個視圖作為子視圖:

let button = UIButton(frame: CGRect(x: 0, y: 0, width: 0, height: 60))
button.setTitle("Add", for: .normal)
buttonView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 60))
buttonView.backgroundColor = .black
button.addSubview(buttonView)

然后為視圖的寬度設置動畫:

UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseIn, animations: {
    self.buttonView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60)
    }, completion: nil)

我對此不滿意的是按鈕標題保持靜態。 我希望它成為動畫的一部分。

我也嘗試為按鈕的框架設置動畫,但似乎不起作用。 什么都沒發生。
我的按鈕已添加到UITextField inputAccessoryView所以我不確定這是否是問題。

有什么建議可以使按鈕正確地動畫嗎?

您不需要UIButton的其他子視圖即可對其框架進行動畫處理。

嘗試在動畫之前和動畫關閉之內添加layoutIfNeeded():

let button = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
button.setTitle("Add", for: .normal)
button.setTitleColor(UIColor.green, for: .normal)
button.backgroundColor = UIColor.black

view.addSubview(button)
button.layoutIfNeeded()

UIView.animate(withDuration: 3, delay: 0.0, options: .curveEaseIn, animations: {
    button.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60)
    button.layoutIfNeeded()
    }, completion: nil)

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM