动画视图的子视图不适用于 CAGradientLayer
Animate view's subViews is not working with CAGradientLayer
我想在我的应用中使用 CAGradientLayer。我在 viewController 的视图子层的索引 0 处插入一个 CAGradientLayer:
class ViewController: UIViewController {
var label: UILabel!
override func viewDidLoad() {
let width = view.frame.size.width
let height = view.frame.size.height
super.viewDidLoad()
label = UILabel(frame: CGRect(x: 30, y: 80, width: width/4, height: height/20))
label.backgroundColor = UIColor.yellow
label.textColor = UIColor.white
label.text = "label"
label.alpha = 0
view.addSubview(label)
let hideButton = UIButton.init(type: .system)
hideButton.frame = CGRect(x: width/2 - 15, y: label.frame.origin.y + 50, width: width/4, height: 30)
hideButton.setTitle("Hide", for: .normal)
hideButton.backgroundColor = UIColor.red
hideButton.addTarget(self, action: #selector(ViewController.fadeOutLabel), for: .touchUpInside)
view.addSubview(hideButton)
let gradientButton = UIButton.init(type: .system)
gradientButton.frame = CGRect(x: width/2 - 15, y: hideButton.frame.origin.y + 50, width: width/4, height: 30)
gradientButton.setTitle("add gradient", for: .normal)
gradientButton.backgroundColor = UIColor.red
gradientButton.addTarget(self, action: #selector(ViewController.updateSubviews), for: .touchUpInside)
view.addSubview(gradientButton)
}
func updateSubviews() {
UIView.animate(withDuration: 0.3, animations: {
self.label.alpha = 1
let gradientSublayer = self.makeGradientLayer()
self.view.layer.insertSublayer(gradientSublayer, at: 0)
}, completion: nil)
}
func makeGradientLayer() -> CAGradientLayer{
let gradientLayer = CAGradientLayer()
gradientLayer.frame = UIScreen.main.bounds
let primaryColor = UIColor.yellow
let secondaryColor = UIColor.green
gradientLayer.colors = [primaryColor.cgColor, secondaryColor.cgColor]
return gradientLayer
}
func fadeOutLabel(){
print("touch received")
UIView.animate(withDuration: 0.3, animations: {
self.label.alpha = 0
}) { (finished) in
print("animation finished")
}
}
}
但是,我在屏幕上还有其他 UI 元素,它们是可见的,但是当我尝试添加一些动画时(在点击按钮或更改其文本时动画标签的 alpha),它不不工作。按钮的选择器触发,然后动画块执行……但什么也没有发生。我尝试了最明显的解决方案——创建一个单独的视图并将其添加为子视图,但结果不会改变。控制器上的所有子视图都被添加到 viewDidLoad 中的主视图。也许我遗漏了一些关于 CALayers 的东西?
在我的原始代码中,我在 viewWillLayoutSubviews() 中添加了标签和其他子视图。我将所有内容移至 viewDidLoad() 并且动画开始工作。不幸的是,我无法在示例项目中重现它。仍然不确定我错了。
我想在我的应用中使用 CAGradientLayer。我在 viewController 的视图子层的索引 0 处插入一个 CAGradientLayer:
class ViewController: UIViewController {
var label: UILabel!
override func viewDidLoad() {
let width = view.frame.size.width
let height = view.frame.size.height
super.viewDidLoad()
label = UILabel(frame: CGRect(x: 30, y: 80, width: width/4, height: height/20))
label.backgroundColor = UIColor.yellow
label.textColor = UIColor.white
label.text = "label"
label.alpha = 0
view.addSubview(label)
let hideButton = UIButton.init(type: .system)
hideButton.frame = CGRect(x: width/2 - 15, y: label.frame.origin.y + 50, width: width/4, height: 30)
hideButton.setTitle("Hide", for: .normal)
hideButton.backgroundColor = UIColor.red
hideButton.addTarget(self, action: #selector(ViewController.fadeOutLabel), for: .touchUpInside)
view.addSubview(hideButton)
let gradientButton = UIButton.init(type: .system)
gradientButton.frame = CGRect(x: width/2 - 15, y: hideButton.frame.origin.y + 50, width: width/4, height: 30)
gradientButton.setTitle("add gradient", for: .normal)
gradientButton.backgroundColor = UIColor.red
gradientButton.addTarget(self, action: #selector(ViewController.updateSubviews), for: .touchUpInside)
view.addSubview(gradientButton)
}
func updateSubviews() {
UIView.animate(withDuration: 0.3, animations: {
self.label.alpha = 1
let gradientSublayer = self.makeGradientLayer()
self.view.layer.insertSublayer(gradientSublayer, at: 0)
}, completion: nil)
}
func makeGradientLayer() -> CAGradientLayer{
let gradientLayer = CAGradientLayer()
gradientLayer.frame = UIScreen.main.bounds
let primaryColor = UIColor.yellow
let secondaryColor = UIColor.green
gradientLayer.colors = [primaryColor.cgColor, secondaryColor.cgColor]
return gradientLayer
}
func fadeOutLabel(){
print("touch received")
UIView.animate(withDuration: 0.3, animations: {
self.label.alpha = 0
}) { (finished) in
print("animation finished")
}
}
}
但是,我在屏幕上还有其他 UI 元素,它们是可见的,但是当我尝试添加一些动画时(在点击按钮或更改其文本时动画标签的 alpha),它不不工作。按钮的选择器触发,然后动画块执行……但什么也没有发生。我尝试了最明显的解决方案——创建一个单独的视图并将其添加为子视图,但结果不会改变。控制器上的所有子视图都被添加到 viewDidLoad 中的主视图。也许我遗漏了一些关于 CALayers 的东西?
在我的原始代码中,我在 viewWillLayoutSubviews() 中添加了标签和其他子视图。我将所有内容移至 viewDidLoad() 并且动画开始工作。不幸的是,我无法在示例项目中重现它。仍然不确定我错了。