NSButtonCellクラスをサブクラス化して、ボタンの外観を変えました。描画コードは正常に機能しています。これは、iTunesの再生コントロールのように見えるCGGradientsで満たされた丸いボタンです。それらは完全に同じではありませんが、私には十分に似ています。
今私の問題はボタンの種類です。でボタンの種類を設定しました-[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];
}