2012-04-01 28 views

回答

12

你可以使用志願

[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]); 
     } 
    } 
} 

你只真正關心的變化,這將被稱爲每次例如狀態變化如果你選擇移動,然後用你的手指仍然向下拖動到按鈕之外

+0

這正是我一直在尋找前的亮狀態!謝謝! – arnoapp 2012-04-01 22:20:36

1

是否這樣?

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

沒有這不起作用,因爲按鈕離開高亮狀態時,它執行TouchUpInside動作之前(至少我是這麼認爲的測試後,猜測... ) – arnoapp 2012-04-01 21:39:11

+1

這不是那樣乾淨,當你可以做這樣的事情拖出按鈕導致按鈕失去它的亮點 – 2012-04-01 21:39:19

+0

要使用拖動外佔優勢的情況下,你可以在最後一行改爲:[self.testButton addTarget :self action:@selector(t2 :) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside]; – 2012-04-01 21:50:22