【问题标题】:Lost in NSButton types and alternate images迷失在 NSButton 类型和替代图像中
【发布时间】:2011-03-16 13:55:43
【问题描述】:

我想要一个NSButton,其中包含一张图片和一张备用图片。按下按钮时应该显示备用图像,我还想通过代码显示备用图像,调用类似[button setSelected:YES] 的代码。如果不手动使用 alternateImage 属性,这可能吗?

【问题讨论】:

    标签: cocoa macos nsbutton


    【解决方案1】:

    这无需手动更改按钮的图像即可:

    在 Interface Builder(xib/nib 编辑器)中将 NSButton 的 Type 设置为 "Toggle",图像会自动变为备用图片/标题。

    【讨论】:

      【解决方案2】:

      我只是在界面编辑器的属性检查器中构建了所有 10 种类型的按钮,没有添加任何代码。结果如下:

      button type while held down when released
      Momentary Push-In first image darkened first image
      Momentary Light first image darkened first image
      Momentary Change ALT IMAGE first image
      Push On / Push Off first image darkened first image
      On / Off first image darkened first image
      Toggle ALT IMAGE ALT IMAGE
      Switch ALT IMAGE ALT IMAGE
      Radio (not valid)
      Accelerator first image darkened first image
      Multi Level Acc. first image darkened first image

      再次按下时,切换和切换将恢复为原始图像。 (如果您在按钮的属性检查器中将按钮类型更改为 Radio,它会恢复为 Switch。)

      【讨论】:

        【解决方案3】:

        您可以使用类型设置为NSToggleButtonNSButton,然后使用NSOnState / NSOffStateNSButton 状态在imagealternateImage 之间切换。

        NSButton* theButton = [[NSButton alloc] initWithFrame:....];
        theButton.image = .....
        theButton.alternateImage = .....
        theButton.state = NSOffState; // displays the image
        theButton.state = NSOnState; // displays the alternateImage
        

        【讨论】:

          【解决方案4】:

          最简单的方法是在两张图片之间切换:

          @implementation NSButton (Select)
          
          - (void) setSelected: (BOOL) yn
          {
              NSImage *const tmp = [self image];
              [self setImage:[self alternateImage]];
              [self setAlternateImage:tmp];
          }
          
          @end
          

          【讨论】:

          • 谢谢,你可能是对的。我添加了一个简单的类别来在图像之间切换。
          • 知道如何为悬停事件设置备用图像(当鼠标悬停在按钮上时,而不是单击时)?
          • @ThEuSeFuL :您必须为按钮使用trackingArea,如果您的按钮类型是NSToggleButton,您可以使用theButton.state = NSOffState; 和`theButton.state 在标准图像和备用图像之间切换= NSOnState;``
          猜你喜欢
          • 2013-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多