动画Uibutton配置更改
现代 UIButton
配置 API 允许新设置按钮外观的方式。然而,下面的代码完全跳过动画。
@IBAction func buttonTouched(_ sender: UIButton) {
sender.configuration?.background.backgroundColor = .green
UIView.animate(withDuration: 0.4, delay: 0.5) {
sender.configuration?.background.backgroundColor = .systemMint
}
}
类似的事情可以这样完成:
@IBAction func buttonTouched(_ sender: UIButton) {
sender.backgroundColor = .red
UIView.animate(withDuration: 0.4, delay: 0.5) {
sender.backgroundColor = .systemMint
}
}
这有效。问题是如何为 UIButton
的配置更改设置动画。
Modern UIButton
configuration API allows for new way of setting button appearance. However the code below skips the animation entirely.
@IBAction func buttonTouched(_ sender: UIButton) {
sender.configuration?.background.backgroundColor = .green
UIView.animate(withDuration: 0.4, delay: 0.5) {
sender.configuration?.background.backgroundColor = .systemMint
}
}
Similar thing can be done like this:
@IBAction func buttonTouched(_ sender: UIButton) {
sender.backgroundColor = .red
UIView.animate(withDuration: 0.4, delay: 0.5) {
sender.backgroundColor = .systemMint
}
}
And this works. The question is how to animate UIButton
's configuration changes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
快速搜索后,它将出现
configuration.background.backgroundcolor
是不是 andimable。根据您的需求,您可以使用
.customview
对按钮的背景使用,然后为该视图对颜色更改进行动画。快速示例(假设您已将按钮添加为
@iboutlet
):After some quick searching, it appears
configuration.background.backgroundColor
is not animatable.Depending on your needs, you can use a
.customView
for the button's background and then animate the color change for that view.Quick example (assuming you've added the button as an
@IBOutlet
):通过使用过渡来实现动画配置更改的方法是迄今为止我能找到的唯一解决方法。有点像这样:
当我尝试优化和重用代码时,我得到了类似的结果:
The way to achieve animated configuration change by using transitions is the only workaround I could find so far. Somewhat like this:
When I tried to optimize and reuse code, I ended with something like: