首页 > 解决方案 > 在 Swift 中为 UIButton 的文本添加基本的发光效果

问题描述

我的问题很直接。我的目标是快速为按钮添加基本的发光效果。我希望文本发光,而不是整个按钮框。我附上了一张图片作为例子来说明我的目标。带有黄色发光效果的 Agency FB 字体的图像。

我在别处看过,但通常只找到不是我想要的动画。抱歉,图片质量很差。

我目前正在使用此代码,但我的设置按钮出现非常微弱的光芒,我怎样才能使它更强大:

import UIKit

extension UIView {
    enum GlowEffect: Float {
        case small = 0.4, normal = 2, big = 30
    }

    func doGlowAnimation(withColor color: UIColor, withEffect effect: GlowEffect = .normal) {
        layer.masksToBounds = false
        layer.shadowColor = color.cgColor
        layer.shadowRadius = 0
        layer.shadowOpacity = 1
        layer.shadowOffset = .zero

        let glowAnimation = CABasicAnimation(keyPath: "shadowRadius")
        glowAnimation.fromValue = 20 // effect.rawValue
        glowAnimation.toValue = 20
        glowAnimation.fillMode = .removed
        glowAnimation.repeatCount = .infinity
        layer.add(glowAnimation, forKey: "shadowGlowingAnimation")
    }
}

改变强度不会在单个字母附近产生强烈的色彩效果

标签: swiftuibuttonglow

解决方案


推荐阅读