动画CALayer border alpha

时间:2018-06-09 08:52:22

标签: ios iphone swift cocoa-touch calayer

我有一个带边框的UIView(颜色:绿色,宽度:10)。

我试图将边界的alpha(在一个循环中)从1.0的值动画到0.2的值 - 然后再回到1.0 - 然后回到0.2等...

但是CALayer没有borderAlpha的属性,所以我不知道该怎么做。

我已尝试过此代码,但它无法正常工作:

UIView.animate(withDuration: 1, delay: 0, options: [.repeat, .autoreverse], animations: {
    self.layer.borderColor = UIColor(cgColor: self.layer.borderColor!).withAlphaComponent(0.2).cgColor
}, completion: nil)

有人知道我该怎么办?

谢谢!

2 个答案:

答案 0 :(得分:0)

更新并简化。使用CALayer创建边框图层,然后使用CABasicAnimation来实现淡入效果:

class BorderView: UIView {
    private var boarderLayer:CALayer?
    private let animationKey = "opacityAnimation"

    override public var frame: CGRect {
        didSet{
            self.updateBorder()
        }
    }

    func updateBorder() {
        if boarderLayer == nil {
            boarderLayer = CALayer()
            boarderLayer?.borderColor = UIColor.red.cgColor
            boarderLayer?.borderWidth = 5.0
            self.layer.addSublayer(boarderLayer!)
        }

        boarderLayer?.frame = self.bounds

        if (boarderLayer?.animation(forKey: animationKey) == nil) {
            self.addAnimiation(layer: boarderLayer!,increasing:true)
        }
    }

    func addAnimiation(layer:CALayer,increasing:Bool) {
        CATransaction.begin()

        CATransaction.setCompletionBlock{ [weak self] in
            self?.addAnimiation(layer: layer,increasing:!increasing)
        }

        let animation = CABasicAnimation(keyPath: "opacity")
        if increasing {
            layer.opacity = 0.2
            animation.fromValue = 1.0
            animation.toValue = 0.2
        }
        else{
            layer.opacity = 1.0
            animation.fromValue = 0.2
            animation.toValue = 1.0
        }
        animation.duration = 1.0
        layer.add(animation, forKey: animationKey)

        CATransaction.commit()
    }
}

结果:

答案 1 :(得分:0)

尝试使用此

class animateStack: UIViewController {

    @IBOutlet weak var animateView: UIView!{
        didSet{
            animateView.layer.borderColor = UIColor.black.cgColor
            animateView.layer.borderWidth = 10
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        animateBorderAlpha()
    }

    private func animateBorderAlpha(){
        /// First Animation
        let animation = CABasicAnimation(keyPath: "borderColor")
        animation.beginTime = 0
        animation.toValue = UIColor.black.withAlphaComponent(0.1).cgColor
        animation.fromValue = UIColor.black.cgColor
        animation.duration = 2

        /// Second Animation
        let animation1 = CABasicAnimation(keyPath: "borderColor")
        animation1.toValue = UIColor.black.cgColor
        animation1.fromValue = UIColor.black.withAlphaComponent(0.1).cgColor
        animation1.beginTime = animation.beginTime + animation.duration
        animation.duration = 4

        /// Animation Group
        let borderColorAnimation: CAAnimationGroup = CAAnimationGroup()
        borderColorAnimation.animations = [animation, animation1]
        borderColorAnimation.duration = animation.duration + animation1.duration
        borderColorAnimation.repeatCount = Float.greatestFiniteMagnitude
        self.animateView.layer.add(borderColorAnimation, forKey: "borderColor")
    }

}

<强>更新

class animateViewClass: NSObject {
    class func animateBorderAlpha(_ view: UIView){
        /// First Animation
        let animation = CABasicAnimation(keyPath: "borderColor")
        animation.beginTime = 0
        animation.toValue = UIColor.black.withAlphaComponent(0.1).cgColor
        animation.fromValue = UIColor.black.cgColor
        animation.duration = 2

        /// Second Animation
        let animation1 = CABasicAnimation(keyPath: "borderColor")
        animation1.toValue = UIColor.black.cgColor
        animation1.fromValue = UIColor.black.withAlphaComponent(0.1).cgColor
        animation1.beginTime = animation.beginTime + animation.duration
        animation.duration = 4

        /// Animation Group
        let borderColorAnimation: CAAnimationGroup = CAAnimationGroup()
        borderColorAnimation.animations = [animation, animation1]
        borderColorAnimation.duration = animation.duration + animation1.duration
        borderColorAnimation.repeatCount = Float.greatestFiniteMagnitude
        view.layer.add(borderColorAnimation, forKey: "borderColor")
    }
}

<强>用法

    animateViewClass.animateBorderAlpha(viewName)
    /// Case of Subclass UIView
    animateViewClass.animateBorderAlpha(self)