Call method when Button leaves Highlighted State

2 Solutions Collect From Internet About “Call method when Button leaves Highlighted State”

You could use KVO

[button addObserver:self forKeyPath:@"highlighted" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    if ([@"highlighted" isEqualToString:keyPath]) {

        NSNumber *new = [change objectForKey:@"new"];
        NSNumber *old = [change objectForKey:@"old"];

        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 this?

self.testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.testButton addTarget:self action:@selector(methodOne) forControlEvents:UIControlEventTouchDown];
[self.testButton addTarget:self action:@selector(methodTwo) forControlEvents:UIControlEventTouchUpInside];