简单的mouseover效果对NSButton [英] Simple mouseover effect on NSButton
本文介绍了简单的mouseover效果对NSButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我为自定义渲染创建了一个自定义 NSButtonCell
。
I am creating a custom NSButtonCell
for a custom rendering.
现在,我想有不同的方面,取决于鼠标是否在按钮上。如何获取此信息?
Now, I want to have different aspect depending if the mouse is over the button or not. How can I get this information?
感谢和问候,
推荐答案
步骤1:创建带跟踪区域的按钮
Step 1: Create the Button with tracking area
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");
}
这篇关于简单的mouseover效果对NSButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文