2

我进行了子类NSButton化,以便单击按钮图像时会发生变化。以下代码用于子类。

#import "MyImageButton2.h"

@interface MyImageButton2 ()

@property (strong, nonatomic) NSCursor *cursor;

@end

@implementation MyImageButton2

- (void)setImage:(NSImage *)image {

    [super setImage:image];

    if (!self.image) {
        self.image = [NSImage imageNamed:@"buttonicon"];
    }
}

- (void)mouseDown:(NSEvent *)theEvent {

    [super mouseDown:theEvent];

    self.image = [NSImage imageNamed:@"buttonicon2"];
}

- (void)mouseUp:(NSEvent *)theEvent {

    [super mouseUp:theEvent];

    self.image = [NSImage imageNamed:@"buttonicon"];
}

- (void)resetCursorRects {

    NSCursor *cursor = (self.cursor) ? self.cursor : [NSCursor pointingHandCursor];

    if (cursor) {
        [self addCursorRect:[self bounds] cursor:cursor];
    } else {
        [super resetCursorRects];
    }
}

@end

这是当前按钮。正如你所看到的,按钮在被点击后并没有返回到它的原始图像,mouseUp事件。

在此处输入图像描述

关于为什么在活动期间图像没有恢复到原始状态的任何想法mouseUp

4

1 回答 1

2

你真的不需要子类NSButton来完成这个。它可以在您的 XIB 文件中轻松完成。在 Interface Builder 属性检查器中,选择您的按钮并将 设置TypeMomentary Change,然后输入buttoniconunderImagebuttonicon2under Alternate

于 2014-05-03T04:05:09.707 回答