簡體   English   中英

iPhone iOS 4 UIButton開啟和關閉突出顯示狀態

[英]iPhone iOS 4 UIButton toggle highlighted state on and off

我在iPhone 4應用程序的界面生成器中設置了樣式為“ Info Dark”的UIButton。 按鈕的屬性之一是“突出顯示”,它在按鈕周圍顯示白色高光。

我想打開和關閉此白色高亮顯示,以指示按鈕功能是否處於活動狀態。

該按鈕通過以下回調鏈接到界面構建器中的“在內部向上觸摸”事件:

infoButton.highlighted = !infoButton.highlighted;

第一次觸摸后,突出顯示消失,並且沒有像我期望的那樣切換。 我還需要做些什么來使高亮顯示切換並顯示按鈕的狀態?

謝謝!

更新:從界面構建器加載后,即使視圖出現/消失,按鈕也保持突出顯示狀態。 導致這種情況發生的原因是“顯示突出顯示”界面構建器屬性。 如果我將上面的代碼分配給另一個按鈕,則信息按鈕將按預期突出顯示打開和關閉。 但是,信息按鈕本身的觸摸會干擾上述代碼,從而導致該按鈕失去“觸摸”突出顯示的功能

更新2:我在界面構建器中的第一個信息按鈕的正下方添加了另一個信息按鈕,並使其永久發光。 為了創建切換的外觀,我隱藏並取消隱藏了真實按鈕下方的glowInfoButton。 這按預期工作:

    infoButton.highlighted = NO;
    glowInfoButton.highlighted = YES;
    glowInfoButton.enabled = NO;
    glowInfoButton.hidden = YES;

- (IBAction)toggleInfoMode:(id)sender {
//    infoButton.selected = !infoButton.selected;
    glowInfoButton.hidden = !glowInfoButton.hidden;
 }

Highlighted圖像是在按下UIButton時Highlighted圖像,並在UIButton本身內進行控制。

您正在尋找Selected屬性。 您可以在IB中設置“選定圖像”,然后放入infoButton.selected = !infoButton.isSelected; 在您的TouchUpInside回調中。

高亮顯示的屬性不能那樣工作,按鈕不能切換。

只是我想知道按鈕是否被按下。

如果要實現該功能,建議您將UIButton或UIControl子類化。

UIButton的突出顯示狀態只是將按鈕的alpha設置為0.5f。 因此,如果將按鈕設置為不更改高亮顯示,則只需在0.1和0.5之間切換Alpha。

例如:

- (void)buttonPressed:(id)sender {

    if((((UIButton*)sender).alpha) != 1.0f){

        [((UIButton*)sender) setAlpha:1.0f];
    } else {
        [((UIButton*)sender) setAlpha:0.5f];
    }

}

也許您真正想要的是

infoButton.enabled = NO;

設置為“否”時,此按鈕將使按鈕變暗並禁用觸摸;設置為“是”時,將允許正常操作。

或者您的情況:

infoButton.enabled = !infoButton.isEnabled;

切換相同的可用性。

如果將它放在您的touchupinside事件中,那么它當然只會在第一次運行。 此后將被禁用,並且不會接收觸摸事件。 您將其放入另一種方法中,該方法確定是否應啟用該按鈕。

如果您確實希望每次按下它都更改一次,則可能應該使用開關,或者可以查看-imageForState,-setTitle:forState和/或-setTitleColor:forState方法。 如果要在每次觸摸時切換外觀,則可以更改外觀。

在建議子類UIButton並檢查對事件的調用然后相應地切換高亮狀態之后,現在我了解了您的真實情況。 您可以執行此操作而無需添加虛擬按鈕。

在自定義按鈕類實現文件中,放置以下代碼或類似內容:

#import "HighlightedButton.h"

@implementation HighlightedButton

BOOL currentHighlightState;

-(void)toggleHighlight:(id)sender {
  self.highlighted = currentHighlightState;
}

-(void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
  //get the string indicating the action called
  NSString *actionString = NSStringFromSelector(action);
  //get the string for the action that you want to check for
  NSString *touchUpInsideMethodName = [[self actionsForTarget:target forControlEvent:UIControlEventTouchUpInside] lastObject];

  if ([touchUpInsideMethodName isEqualToString:actionString]){
    //toggle variable
    currentHighlightState = !currentHighlightState;
    //allow the call to pass through
    [super sendAction:action to:target forEvent:event];
    //toggle the property after a delay (to make sure the event has processed)
    [self performSelector:@selector(toggleHighlight:) withObject:nil afterDelay:.2];
  } else {
    //not an event we are interested in, allow it pass through with no additional action
    [super sendAction:action to:target forEvent:event];
  }

}

@end

這是在適當解決方案下的快速運行,您可能不喜歡切換上的閃爍。 我確定您是否會進行一些可以糾正的更改。 我嘗試過,實際上很喜歡您所說的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM