2014-01-05 170 views
0

我需要將UIButton圖像更改爲點擊下的「選定」或「突出顯示」狀態的圖像(而不是點擊結束時)。如何更改按鈕狀態更改時UIButton的圖像?

此外,我需要更改按鈕的狀態自來水「選擇」與代碼:

- (IBAction)convertDown:(id)sender { 
    [buttonConvert setSelected:TRUE]; 
} 

但有了這個代碼,我看到了「正常」狀態的圖像,直至水龍頭結束:(

如何解決這個

回答

0

這是Interface Builder的問題不能那段時間,直到自來水年底設定的圖像可以通過下面的代碼是固定的:。

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [buttonConvert setBackgroundImage:[UIImage imageNamed:@"iphone_button_highlighted.png"] 
     forState:UIControlStateSelected | UIControlStateHighlighted]; 
} 
+0

也可以在Interface Builder中做到這一點。這與我所說的一樣。 – matt

+0

沒有UIControlStateSelected | UIControlStateHighlighted'狀態。 – Dmitry

+0

好的,我收回!我只是想退後一步,讓這種事情自行解決。 – matt

0

指定爲您的按鈕不同勢目標ControlEvents

有許多ControlEvents可供選擇:

UIControlEventTouchDown   = 1 << 0, 
    UIControlEventTouchDownRepeat  = 1 << 1, 
    UIControlEventTouchDragInside  = 1 << 2, 
    UIControlEventTouchDragOutside = 1 << 3, 
    UIControlEventTouchDragEnter  = 1 << 4, 
    UIControlEventTouchDragExit  = 1 << 5, 
    UIControlEventTouchUpInside  = 1 << 6, 
    UIControlEventTouchUpOutside  = 1 << 7, 
    UIControlEventTouchCancel   = 1 << 8, 

    UIControlEventValueChanged  = 1 << 12, 

    UIControlEventEditingDidBegin  = 1 << 16, 
    UIControlEventEditingChanged  = 1 << 17, 
    UIControlEventEditingDidEnd  = 1 << 18, 
    UIControlEventEditingDidEndOnExit = 1 << 19, 

    UIControlEventAllTouchEvents  = 0x00000FFF, 
    UIControlEventAllEditingEvents = 0x000F0000, 
    UIControlEventApplicationReserved = 0x0F000000, 
    UIControlEventSystemReserved  = 0xF0000000, 
    UIControlEventAllEvents   = 0xFFFFFFFF 

[yourButton addTarget:self 
      action:@selector(methodTouchUpInside:) 
forControlEvents: UIControlEventTouchUpInside]; 

- (void)methodTouchDown:(id)sender{ 

    NSLog(@"TouchDown"); 
} 

或添加gustures,而不是分配目標ControlEvents

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; 
[yourButton addGestureRecognizer: tapGesture]; 

- (void) handleTap:(UITapGestureRecognizer *)sender 
{ 
    if(sender.state == UIGestureRecognizerStateBegan) 
    { 
    NSLog(@"UIGestureRecognizerStateBegan"); 
    } 
    else if(sender.state == UIGestureRecognizerStateEnded) 
    { 
    NSLog(@"UIGestureRecognizerStateEnded"); 
    } 
} 

有很多UIGestureRecognizerState可用。

UIGestureRecognizerStatePossible, 
    UIGestureRecognizerStateBegan, 
    UIGestureRecognizerStateChanged, 
    UIGestureRecognizerStateEnded, 
    UIGestureRecognizerStateCancelled, 
    UIGestureRecognizerStateFailed, 
    UIGestureRecognizerStateRecognized