-1

I've incorporated a glow effect into my UIButtons, (code taken from https://github.com/bc-oscar/Glowing-UIButton). The code is in a separate swift file and the buttons are defined in the view controller as being of type 'GlowingButton', and changed to derive from this custom class in the identity inspector. The effect works when the app is run the first time but then stops when returning to it after exit?

import UIKit 

class GlowingButton: UIButton {

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
    // Drawing code
}
*/

// Creates a glow effect in the button by setting its layer shadow properties
func startGlowWithCGColor (growColor:CGColor) {
    self.layer.shadowColor = growColor
    self.layer.shadowRadius = 10.0
    self.layer.shadowOpacity = 1.0
    self.layer.shadowOffset = CGSizeZero
    self.layer.masksToBounds = false

    // Autoreverse, Repeat and allow user interaction.
    UIView.animateWithDuration(1.0, delay: 0, options: [UIViewAnimationOptions.Autoreverse,                                                         UIViewAnimationOptions.CurveEaseInOut, UIViewAnimationOptions.Repeat, UIViewAnimationOptions.AllowUserInteraction],
        animations: { () -> Void in
            // Make it a 15% bigger
            self.transform = CGAffineTransformMakeScale(1.15, 1.15)
        }) { (Bool) -> Void in
            // Return to original size
            self.layer.shadowRadius = 0.0
            self.transform = CGAffineTransformMakeScale(1.0, 1.0)
    }
}

// Removes the animation
func stopGlow () {
    self.layer.shadowRadius = 0.0
    self.transform = CGAffineTransformMakeScale(1.0, 1.0)
    self.layer.removeAllAnimations()
    self.layer.masksToBounds = true
}

}

Tim
  • 583
  • 1
  • 6
  • 18

2 Answers2

0

That's because you are calling startGlowWithCGColor in your viewDidLoad, try putting it in viewDidAppear

https://github.com/bc-oscar/Glowing-UIButton/blob/master/SampleApp/Sample%20App/ViewController.swift#L24

tpae
  • 6,286
  • 2
  • 37
  • 64
  • I made a `viewDidAppear` function and put the `button.startGlowWithCGColor` calls in there, but it still didn't work. – Tim Dec 15 '15 at 01:09
  • Neither answer nor comment has helped me so far. – Tim Dec 15 '15 at 10:21
0

I don't fully understand what causes this but it seems exiting the app leaves the function stuck on self.transform = CGAffineTransformMakeScale(1.0, 1.0). Recursively calling the function with self.startGlowWithCGColor(growColor) restarts the effect.

Tim
  • 583
  • 1
  • 6
  • 18