繁体   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