动画后如何更新 CAGradientlayer 大小? [英] How to update CAGradientlayer size after animation?

查看:58
本文介绍了动画后如何更新 CAGradientlayer 大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 UIText 创建了一个扩展类来创建渐变层.在为 UIText 大小设置动画后,渐变层大小变得小于作为父元素的 UIText itef.这是我的代码:

I created an extension class for UIText to create a gradient layer. After animating the UIText size the gradient layer size gets smaller than the UIText itsef that is the parent element. Here's my code:

extension UITextField {
func gradientBackground(firstColor: UIColor, secondColor: UIColor){
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = super.bounds
    gradientLayer.colors = [firstColor.cgColor, secondColor.cgColor]
    gradientLayer.locations = [0.0, 1.0]
    gradientLayer.startPoint = CGPoint(x:0.0, y:0.0)
    gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)
    layer.addSublayer(gradientLayer)  
}

class gradientTeLabel: UITextField {
    var once = true
    override func layoutSubviews() {  
        super.layoutSubviews()
        if once {
            self.gradientBackground(firstColor: UIColor(red: 0.30, green: 0.55, blue: 1.00, alpha: 1), secondColor: UIColor(red: 0.00, green: 0.36, blue: 1.00, alpha: 1))
            once = false
        }
    }

}

---------这些在单独的文件中--------

---------these were are in separate file--------

  @IBAction func showMarkUp(_ sender: Any) {
        if mark1.isHidden {
            _ = UIViewPropertyAnimator(duration: 0.25, curve: .easeInOut){

            self.expandButton.transform = CGAffineTransform(rotationAngle: .pi)
            self.asdf.layer.frame = self.asdf.bounds
                }.startAnimation()
            asdf.layer.frame = asdf.bounds
        } else {
            _ = UIViewPropertyAnimator(duration: 0.250, curve: .easeInOut){

            self.mark1.isHidden = true
                         }.startAnimation()
        }
    }

---------这些在 ViewController 中--------

---------these are in ViewController--------

asdf 是包含渐变层的 UITextField.如何更新渐变层大小以匹配 UIText 大小?

asdf is the UITextField that contains the gradient layer. How can I update the gradient layer size to match the UIText size?

推荐答案

您有两个主要选择.

  • 如果可能,不要添加这样的松散"子层.相反,添加一个子视图,其布局基于父视图(例如,自动调整大小或自动布局),这样当您在超级视图上执行视图动画时,子视图就会随之而来.

  • If possible, do not add "loose" sublayers like this. Instead, add a subview, with layout based on the superview (e.g. autoresizing or autolayout), so that when you perform view animation on the superview, the subview comes along for the ride.

如果你不能这样做,那么当你在视图上做视图动画时,你也必须在层上做层动画(使用核心动画)来匹配它.

If you can't do that, then when you do view animation on the view, you must also do layer animation on the layer (using Core Animation) to match it.

这篇关于动画后如何更新 CAGradientlayer 大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆