25

NSButtonCellカスタム レンダリングのカスタムを作成しています。

ここで、マウスがボタンの上にあるかどうかに応じて、さまざまな側面を持ちたいと考えています。どうすればこの情報を入手できますか?

よろしくお願いいたします。

4

7 に答える 7

36

これが私が作成し、私のために完璧に働いた...

ステップ 1: トラッキング エリア付きのボタンを作成する

 NSButton *myButton = [[NSButton alloc] initWithFrame:NSMakeRect(100, 7, 100, 50)];
[myButton setTitle:@"sample"];

[self.window.contentView addSubview:myButton];
// Insert code here to initialize your application
NSTrackingArea* trackingArea = [[NSTrackingArea alloc]
                                initWithRect:[myButton bounds]
                                options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways
                                owner:self userInfo:nil];
[myButton addTrackingArea:trackingArea];

ステップ: 2 以下のメソッドを実装する

- (void)mouseEntered:(NSEvent *)theEvent{
NSLog(@"entered");
[[myButton cell] setBackgroundColor:[NSColor blueColor]];


}

- (void)mouseExited:(NSEvent *)theEvent{
[[myButton cell] setBackgroundColor:[NSColor redColor]];
NSLog(@"exited");

}
于 2014-02-25T18:39:05.477 に答える
22

スウィフト 3:

コードでボタンを作成するか、@IBOutlet を使用します。次に、マウス オーバー (ホバー) のボタンのトラッキング領域を定義します。

let area = NSTrackingArea.init(rect: yourButtonName.bounds,
                               options: [.mouseEnteredAndExited, .activeAlways], 
                               owner: self, 
                               userInfo: nil)
yourButtonName.addTrackingArea(area)

次に、mouseEntered と mouseExited をオーバーライドし、これらの関数で変更したいもの (ボタンの色、ボタンの画像、ボタンのテキストなど) を設定します。

override func mouseEntered(with event: NSEvent) {
    print("Entered: \(event)")
}

override func mouseExited(with event: NSEvent) {
    print("Exited: \(event)")
}

複数のボタン (それぞれに追跡領域が追加されている) があり、どのボタンが mouseEntered イベントをトリガーしたかを特定する必要がある場合、この目的のためにいくつかの userInfo 情報を追加できます。

userInfo: nil

各ボタンの userInfo にカスタム ボタン名を追加します。次に例を示します。

userInfo: ["btnName": "yourButtonName"]

次に、次のように、mouseEntered および mouseExited 関数に switch-case または if ステートメントを記述できます。

override func mouseEntered(with event: NSEvent) {
        // Identify which button triggered the mouseEntered event
        if let buttonName = event.trackingArea?.userInfo?.values.first as? String {
            switch (buttonName) {
            case "yourButtonName":
                //do whatever you want for this button..
            case "anotherButtonName":
                //do whatever you want for this button..
            default:
                print("The given button name: \"\(buttonName)\" is unknown!")
            }
        }
    }
于 2017-07-08T13:46:00.323 に答える
10

NSButtonクラス(またはさらに良いNSButtonCellクラス)をサブクラス化する必要があります。ジャスティンが言ったように、2つの方法を入れれば

- (void)mouseEntered:(NSEvent *)theEvent;
- (void)mouseExited:(NSEvent *)theEvent;

マウスがエリアに出入りするときに呼び出されるはずです。追跡領域を再作成する必要がある場合もあります。こちらをご覧ください。

- (void)updateTrackingAreas

フェードインとフェードアウトの効果については、たとえばアニメーターとアルファ値で遊んだ。

[self animator]setAlphaValue:0.5]; 
于 2011-05-23T14:52:06.107 に答える
4

コールバック付きのSwift 5バージョン、非常に使いやすい:

final class HoverButton: NSButton {

    private let callback: (HoverButton, Bool) -> Void

    init(callback: @escaping (HoverButton, Bool) -> Void) {
        self.callback = callback
        super.init(frame: .zero)
        let area = NSTrackingArea(rect: bounds, options: [.mouseEnteredAndExited, .activeAlways, .inVisibleRect], owner: self, userInfo: nil)
        addTrackingArea(area)
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func mouseEntered(with event: NSEvent) {
        super.mouseEntered(with: event)
        callback(self, true)
    }

    override func mouseExited(with event: NSEvent) {
        super.mouseExited(with: event)
        callback(self, false)
    }

}

使用法:

let button = HoverButton { button, isHovered in
    button.animator().alphaValue = isHovered ? 1 : 0.5
}
于 2020-07-21T09:29:14.507 に答える
3

NSResponder で宣言された適切な出発点:

- (void)mouseEntered:(NSEvent *)theEvent;
- (void)mouseExited:(NSEvent *)theEvent;

具体的には、ボタン セルのコンテナー (セル自体ではない) が NSResponder です。

于 2011-05-23T09:00:28.627 に答える
0

サブクラス化を好む人は、自分で作成してNSButtonその中に を割り当てることもできますNSTrackingArea

Joey Zhouのおかげで、これは本当にシンプルでエレガントな方法です: https://github.com/Swift-Kit/JZHoverNSButton

これはSwift 2で書かれていますが、XCode は問題なくSwift 3 - 4に自動的に変換します。

それが誰かを助けることを願っています

于 2018-07-26T09:11:28.547 に答える