2

I have some text that I need to add a glow to. The thing is, when adding the glow effect, I get a performance hit.

I have several animations (they happen one at a time only and are nothing fancy or complex). These are animations like changing the alpha and scale values of a UIView.

They are perfetcly smooth! However, when adding a glow with Quartz Core to a text that I have on screen, the rest of the animations stops being so smooth.

On my father's iPhone 3GS, they work great! On mine however, an iPhone 4, they get slow! Documentation warns about the retina display because of having 4x the pixels. But I really need this glow effect!

// 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;

Is there anyway I can do this without affecting performance?

EDIT

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

Thank you!

nmdias
  • 3,888
  • 5
  • 36
  • 59
  • It depends exactly how you are trying to use it. Some bad hacks would be to have a second font that looks like the glow effect, and layer the text, but that may not be very flexible or look very nice – bengoesboom Aug 11 '13 at 23:04
  • Already thought of going down that road! But it just felt too messy for a glow effect. – nmdias Aug 11 '13 at 23:13
  • 1
    Glow/shadow effects are expensive, there's no two ways around that. I think it's unlikely you'll find a better way to do it; if so it would probably have to be openGL directly on the graphics card, and if you have to ask you won't be able to do it better than Quartz2d. – Kevin Aug 11 '13 at 23:17

2 Answers2

0

Make sure you set a shadowPath like this

[myView.layer setShadowPath:[[UIBezierPath 
bezierPathWithRect:myView.bounds] CGPath]];

Reference: On the importance of setting shadowPath

user1459524
  • 3,613
  • 4
  • 19
  • 28
0

Swift 4

Glowing animation with little performance hit. Lets say u have a UILabel or UITextField holding your text.

1- Create UIView extension with the animation

2- Use it on textfields, buttons, views (any subclass of UIView)

UIView extension

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")
    }
}

How to use it on UILabels and Textfields:

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

//Label
label.doGlowAnimation(withColor: label.textColor, withEffect: .small)
alegelos
  • 2,308
  • 17
  • 26