我对 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];
}