Ios 在避免大的性能冲击的同时,是否可以在文本中添加辉光效果?

Ios 在避免大的性能冲击的同时,是否可以在文本中添加辉光效果?,ios,objective-c,quartz-core,Ios,Objective C,Quartz Core,我有一些文本需要添加光晕。问题是,当添加辉光效果时,我的性能会受到影响 我有几个动画(它们一次只发生一个,没有什么新奇或复杂)。这些是类似于更改UIView的alpha和scale值的动画 它们非常光滑!然而,当我在屏幕上的文本中添加一个带有石英核的光晕时,其余的动画就不再那么平滑了 在我父亲的iPhone3GS上,它们工作得很好!然而,在我的iPhone4上,它们变慢了!由于视网膜显示有4倍的像素,文档中会对视网膜显示发出警告。但我真的需要这种发光效果 // This work good!

我有一些文本需要添加光晕。问题是,当添加辉光效果时,我的性能会受到影响

我有几个动画(它们一次只发生一个,没有什么新奇或复杂)。这些是类似于更改UIView的alpha和scale值的动画

它们非常光滑!然而,当我在屏幕上的文本中添加一个带有石英核的光晕时,其余的动画就不再那么平滑了

在我父亲的iPhone3GS上,它们工作得很好!然而,在我的iPhone4上,它们变慢了!由于视网膜显示有4倍的像素,文档中会对视网膜显示发出警告。但我真的需要这种发光效果

// This work good!
    _timerLabel.shadowColor  = [UIColor darkGrayColor];
    _timerLabel.shadowOffset = CGSizeMake(0.0, 0.5);
    _timerLabel.alpha        = 1.0;

// This gets me a performance hit
_timerLabel.layer.shadowRadius  = 3;
_timerLabel.layer.shadowOpacity = 0.3;
我是否可以在不影响性能的情况下执行此操作

编辑

// This does help some! But it's not there yet.. It still has a heavy FPS loss
_timerLabel.layer.shouldRasterize = YES;

谢谢大家!

确保像这样设置
阴影路径

[myView.layer setShadowPath:[[UIBezierPath 
bezierPathWithRect:myView.bounds] CGPath]];
参考资料:Swift 4

光彩夺目的动画,几乎没有性能影响。假设你有一个UILabel或UITextField来保存你的文本

1-使用动画创建UIView扩展

2-在文本字段、按钮、视图(UIView的任何子类)上使用它

UIView扩展

import UIKit

extension UIView{
    enum GlowEffect:Float{
        case small = 0.4, normal = 1, big = 5
    }

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

        let glowAnimation = CABasicAnimation(keyPath: "shadowRadius")
        glowAnimation.fromValue = 0
        glowAnimation.toValue = 1
        glowAnimation.beginTime = CACurrentMediaTime()+0.3
        glowAnimation.duration = CFTimeInterval(0.3)
        glowAnimation.fillMode = kCAFillModeRemoved
        glowAnimation.autoreverses = true
        glowAnimation.isRemovedOnCompletion = true
        layer.add(glowAnimation, forKey: "shadowGlowingAnimation")
    }
}
如何在UILabels和Textfields上使用它:

//TextField with border
textField.doGlowAnimation(withColor: UIColor.red, withEffect: .big)

//Label
label.doGlowAnimation(withColor: label.textColor, withEffect: .small)

这完全取决于你如何使用它。一些坏的黑客会有第二种字体,看起来像辉光效果,并分层的文字,但这可能不是很灵活,或看起来很好,已经想过要走这条路了!但是对于辉光效果来说,它感觉太凌乱了。辉光/阴影效果很昂贵,没有两种解决方法。我认为你不太可能找到更好的方法;如果是这样的话,它可能必须是直接在图形卡上的openGL,如果你不得不问,你将无法做得比Quartz2d更好。