按钮离开突出显示状态时的调用方法
我想在突出显示按钮时执行操作,并在离开突出显示状态时执行其他操作。 任何建议?
你可以使用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]); } } }
你只关心这些变化,每次状态改变时都会调用它,例如你移动选择然后用手指向下拖动按钮外面的按钮
喜欢这个?
self.testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [self.testButton addTarget:self action:@selector(methodOne) forControlEvents:UIControlEventTouchDown]; [self.testButton addTarget:self action:@selector(methodTwo) forControlEvents:UIControlEventTouchUpInside];