ios - 动画 CALayer 边框 alpha

标签 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)

有人知道我该怎么做吗?

谢谢!

最佳答案

尝试使用这个

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)

关于ios - 动画 CALayer 边框 alpha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50772527/

相关文章:

javascript - 如何在 iOS 上使用 cordova/phonegap 更改 img 源

ios - 将文本字段输入限制为设定长度的数字输入

iphone - 如何在 UIView 上通过触摸移动绘制任意路径

html - 如何使用 Swift 和 Kanna 库获取 HTML 表单值

swift - Swift 中的 getter 和 setter

ios - 如何使用 UITextFieldViewModeWhileEditing 检测 UITextField 是否已清除

objective-c - 点击选项卡栏项目 View 在 iphone 中没有改变

iphone - 在 iPhone 上阅读 Cacao(不是 Cocoa Touch)文件

iphone - 如何淡出状态栏而不隐藏它

ios - 具有潜在泄漏的Objective-C Zlib方法