NSButtonCell
カスタム レンダリングのカスタムを作成しています。
ここで、マウスがボタンの上にあるかどうかに応じて、さまざまな側面を持ちたいと考えています。どうすればこの情報を入手できますか?
よろしくお願いいたします。
NSButtonCell
カスタム レンダリングのカスタムを作成しています。
ここで、マウスがボタンの上にあるかどうかに応じて、さまざまな側面を持ちたいと考えています。どうすればこの情報を入手できますか?
よろしくお願いいたします。
これが私が作成し、私のために完璧に働いた...
ステップ 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");
}
スウィフト 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!")
}
}
}
NSButtonクラス(またはさらに良いNSButtonCellクラス)をサブクラス化する必要があります。ジャスティンが言ったように、2つの方法を入れれば
- (void)mouseEntered:(NSEvent *)theEvent;
- (void)mouseExited:(NSEvent *)theEvent;
マウスがエリアに出入りするときに呼び出されるはずです。追跡領域を再作成する必要がある場合もあります。こちらをご覧ください。
- (void)updateTrackingAreas
フェードインとフェードアウトの効果については、たとえばアニメーターとアルファ値で遊んだ。
[self animator]setAlphaValue:0.5];
コールバック付きの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
}
NSResponder で宣言された適切な出発点:
- (void)mouseEntered:(NSEvent *)theEvent;
- (void)mouseExited:(NSEvent *)theEvent;
具体的には、ボタン セルのコンテナー (セル自体ではない) が NSResponder です。
サブクラス化を好む人は、自分で作成してNSButton
その中に を割り当てることもできますNSTrackingArea
。
Joey Zhouのおかげで、これは本当にシンプルでエレガントな方法です: https://github.com/Swift-Kit/JZHoverNSButton
これはSwift 2で書かれていますが、XCode は問題なくSwift 3 - 4に自動的に変換します。
それが誰かを助けることを願っています