NSButtonCell子类'按钮类型不起作用

v1A*_*xvw 1 cocoa drawing subclass nsbuttoncell


我将NSButtonCell类子类化为我的按钮给出了不同的外观.我的绘图代码工作正常.这是一个圆形的按钮,里面装满CGGradients,看起来就像是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
Run Code Online (Sandbox Code Playgroud)

如您所见,我通过该[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];
}
Run Code Online (Sandbox Code Playgroud)

uli*_*ess 24

你看错了房产.-state是否检查按钮.你想看看isHighlighted,它会告诉你按钮是否被按下.

看一个复选框:

复选框状态

当您单击并按住它时,复选框仍未选中,但略微变暗(702).然后当你松开鼠标时,它会检查(704).然后当您再次单击并按住时,选中的复选框变暗,但仍然会被选中(705),并且只有当您在内部释放时,它才会实际取消选中(701).

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

它是您的绘图代码,它查看-showsStateBy属性,并且只显示状态,如果它指示它应该.