Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call method when Button leaves Highlighted State

I want to perform a action when a button is highlighted and perform another action when it leaves the highlighted state. Any advice?

like image 683
arnoapp Avatar asked Dec 09 '22 01:12

arnoapp


1 Answers

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

like image 190
Paul.s Avatar answered Dec 25 '22 13:12

Paul.s