Question个
我正在try 将UIView的 colored颜色 从浅灰色设置为深灰色,将UIButton标题的 colored颜色 从蓝色设置为橙色.
当点击UIButton时,动画将在3秒内发生.
UIView会在3秒内改变 colored颜色 ,但UIButton不会,并且会立即改变 colored颜色 .
-
为什么UIButton标题的彩色动画没有达到预期的效果?
-
如何特别使用UIView.Animate使动画工作?
谢谢.
Code个
import UIKit
class ViewController: UIViewController {
let button = UIButton(type: .system)
override func viewDidLoad() {
super.viewDidLoad()
// Set view colour
self.view.backgroundColor = .lightGray
// Set initial title color
button.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
button.setTitle("Button", for: .normal)
button.setTitleColor(.blue, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 50)
view.addSubview(button)
// Add tap gesture recognizer to trigger animation
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(buttonTapped))
button.addGestureRecognizer(tapGesture)
}
@objc func buttonTapped() {
// Animate the change of view and title color
UIView.animate(withDuration: 3, delay: 0, options: [.beginFromCurrentState, .curveEaseInOut], animations: {
self.view.backgroundColor = .darkGray
self.button.setTitleColor(.orange, for: .normal)
})
}
}