Creating a custom animation for a tvOS UIButton press

I am attempting to create a custom button press animation for a tvOS button, I have already created a didUpdateFocusInContext function for focus update animations. I tried to overload the highlighted variable didSet to animate the press of the button (its custom, not system), but what happens is it works mostly, but there is one flaw. When you press down, then move the focus off of the button, it never fires the highlighted didSet for setting it to false from focusing off of the button while down. Does this mean it stays highlighted? Doesn't seem like it. Any suggestions? Thanks!


class UIButtonTV: UIButton {


override var highlighted : Bool {

didSet {

if ( highlighted == true ) {

UIView.animateWithDuration(0.07) {

self.setTitleColor(UIColor.whiteColor(), forState: .Normal)

}

} else {

UIView.animateWithDuration(0.07) {

self.setTitleColor(UIColor.blackColor(), forState: .Normal)

}


}

}

}

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {

super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)

if(context.nextFocusedView == self){

coordinator.addCoordinatedAnimations({

self.transform = CGAffineTransformMakeScale(1.25, 1.25)

self.layoutIfNeeded()

},

completion: nil

)


} else if (context.previouslyFocusedView == self) {

coordinator.addCoordinatedAnimations({

self.transform = CGAffineTransformMakeScale(1.0, 1.0)

self.layoutIfNeeded()

},

completion: nil

)


}

}


}

Did some tests with this, it appears that when you focus off of the button while its down, the highlighted flag DOES IN FACT get set to false, but without didSet or willSet ever being called. I think this could constitute as a bug? Or maybe something has to do with how I used the override, and a super class is being referenced inside the system when setting it to false when focusing off?


Anyhow I just slipped the unhighlight animation into the loss focus animation as well, and that seems to be a quick band aid for the issue.

Creating a custom animation for a tvOS UIButton press
 
 
Q