1


我对 NSButtonCell 类进行了子类化,以使我的按钮具有不同的外观。我的绘图代码工作正常。这是一个充满 CGGradient 的圆形按钮,看起来像 iTunes 播放控件。它们并不完全相同,但它们对我来说足够相似。

现在我的问题是按钮类型。我在中设置了按钮类型,-[PlayerButtonCell initImageCell:]但我没有让它工作,只在按下按钮时绘制推入的外观。我向您展示我的一段代码:

//
//  PlayerButtonCell.m
//  DownTube
//

#import "PlayerButtonCell.h"

@implementation PlayerButtonCell
/* MARK: Init */
- (id)initImageCell:(NSImage *)image {
    self = [super initImageCell:image];
    if(self != nil) {
        [self setImage:image];
        [self setButtonType:NSMomentaryPushInButton];
    }
    return self;
}

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    NSImage *myImage;
    NSBezierPath *myPath;
    NSRect myFrame;
    NSInteger myState;

    myFrame = NSInsetRect(cellFrame , STROKE_WIDTH / 2.0 , STROKE_WIDTH / 2.0);
    myImage = [self image];
    myState = [self state];

    NSLog(@"%d", [self buttonType]);

    /* Create bezier path */
    {
        myPath = [NSBezierPath bezierPathWithOvalInRect:myFrame];
    }

    /* Fill with background color */
    {
        /* Fill code here */
    }


    /* Draw gradient if on */
    if(myState == NSOffState) {
        /* Code to draw the button when it's not pushed in */

    } else {
        /* Code to draw the button when it IS pressed */
    }

    /* Stroke */
    {
        /* Code to stroke the bezier path's edge. */
    }
}
@end

如您所见,我通过该[NSButtonCell state]方法检查了推入状态。但纽扣电池就像一个复选框,如NSSwitchButton. 这个,我不想要。我想让它瞬间点亮。

希望有人可以帮助我,
ief2

编辑:我用这个代码创建了一个按钮,它有任何用处:

- (NSButton *)makeRefreshButton {
    NSButton *myButton;
    NSRect myFrame;
    NSImage *myIcon;
    PlayerButtonCell *myCell;

    myIcon = [NSImage imageNamed:kPlayerViewRefreshIconName];
    myCell = [[PlayerButtonCell alloc] initImageCell:myIcon];

    myFrame.origin = NSMakePoint(CONTENT_PADDING , CONTENT_PADDING);
    myFrame.size = CONTROL_PLAYBACK_SIZE;

    myButton = [[NSButton alloc] initWithFrame:myFrame];
    [myButton setCell:myCell];
    [myButton setButtonType:NSMomentaryPushInButton];

    [myCell release];

    return [myButton autorelease];
}
4

2 回答 2

25

您正在查看错误的属性。-state 是按钮是否被选中。您想查看 isHighlighted,它会告诉您按钮是否被按下。

看一个复选框:

复选框状态

当您单击并按住它时,该复选框仍未选中,但会稍微变暗(702)。然后当您释放鼠标时,它会检查 (704)。然后当您再次单击并按住时,已选中的复选框变暗,但仍处于选中状态(705),并且只有当您在内部释放时,它才会真正取消选中(701)。

所以有两个不同的方面,高亮和状态,它们结合起来提供了 4 种外观之一。每个 NSButtonCell 都会将其状态提升到 -nextState 在单击时返回的任何值。但有些按钮类型显示状态,而另一些则不显示。查看任何按钮(例如对话框窗口中的“确定”按钮),您会发现在幕后它的状态在开和关之间变化,即使它总是以相同的方式绘制。

您的绘图代码会查看 -showsStateBy 属性,并且仅在表明应该显示状态时才显示状态。

于 2012-10-17T13:46:54.127 回答
1

你试过[self setButtonType:NSMomentaryLightButton];初始化吗?

于 2011-06-19T06:44:00.433 回答