繁体   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