title: Adding a CABasicAnimation to a subclassed UIButton - if enabled
body: <p>I am having problems with Autolayout and a subclassed UIButton. In my UIButton subclass, I override isEnabled to add a color animation when the button is enabled. See below.</p>
  
  <pre><code>// Within my subclassed button:
  override var isEnabled:Bool {
      didSet {
          UIChanges.colorButton(buttonToFade: self)
      }
  }
  
  // colorButton to be called from UIChanges:
  static func colorButton(buttonToFade:UIView) {
      let colorAnimation = CABasicAnimation(keyPath: "backgroundColor")
      colorAnimation.duration = 1
      colorAnimation.fromValue = UIColor.black.cgColor
      colorAnimation.toValue = UIColor.white.cgColor
      colorAnimation.repeatCount = 10
      colorAnimation.autoreverses = true
      buttonToFade.layer.add(colorAnimation, forKey: nil)
  }
  </code></pre>
  
  <p>The problem is, this animation never occurs.</p>
  
  <p><strong>How can I add this colorButton() animation to the subclassed button <em>only</em> if it's enabled?</strong></p>
  
  <p>I presume this has something to do with autolayout, because if I put that function within layoutSubviews, it works fine.</p>
  
  <p>EDIT:
  I added a new button to the View Controller that manually changes the subclassed button to enabled, and @JD. answer works fine (the CABasicAnimation fires). However, the CABasicAnimation does not fire if that tester button isn't pressed. This button I am trying to adjust gets enabled mostly in AppDelegate - so could this be a cause of the problem? The button frame isn't loaded when the button gets sets to enabled? Autolayout issue?</p>