質問


NSButtonCellクラスをサブクラス化して、ボタンを別の外観にしました。図面コードが正常に動作しています。 iTunesの再生コントロールのように見えるように、cgradientsで満たされた丸いボタンです。それらは完全に同じではありませんが、私には十分に似ています。

これで私の問題はボタンタイプです。にボタンタイプを設定します -[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];
}

正しい解決策はありません

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top