I want to perform a action when a button is highlighted and perform another action when it leaves the highlighted state. Any advice?
You could use KVO
[button addObserver:self forKeyPath:@"highlighted" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
Then
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([@"highlighted" isEqualToString:keyPath]) {
NSNumber *new = [change objectForKey:NSKeyValueChangeNewKey];
NSNumber *old = [change objectForKey:NSKeyValueChangeOldKey];
if (old && [new isEqualToNumber:old]) {
NSLog(@"Highlight state has not changed");
} else {
NSLog(@"Highlight state has changed to %d", [object isHighlighted]);
}
}
}
You only really care about the changes and this will be called every time the state changes e.g. if you move select and then with your finger still down drag outside of the button
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With