Jos*_*iah 4 cocoa objective-c mouseevent mousehover
我有一个Mac应用程序,在窗口的每个底角有两个NSButton.我已将它们设置Alpha = .5
在.xib中.我想知道如何检测鼠标悬停在按钮上并将alpha更改为.9.我该怎么做呢?我想从你的代码中你需要知道的就是.h.
@property (unsafe_unretained) IBOutlet NSButton *leftButton;
@property (unsafe_unretained) IBOutlet NSButton *rightButton;
Run Code Online (Sandbox Code Playgroud)
那些是在我的.m中合成的.部署目标是OS X 10.6+.谢谢.
编辑
我真的没有尝试过任何东西.我在NSTrackingArea上看过一些东西,但对如何使用它并不积极,更重要的是,我想知道是否还有其他方法可以解决它.例如已经调用的事件或其他事件.
您需要对NSButton类进行子类化(甚至更好的是NSButtonCell类).
- (void)mouseEntered:(NSEvent *)theEvent;
- (void)mouseExited:(NSEvent *)theEvent;
Run Code Online (Sandbox Code Playgroud)
当鼠标进入和退出该区域时,应该调用它们.您可能还需要重新创建跟踪区域,请在此处查看:
- (void)updateTrackingAreas
Run Code Online (Sandbox Code Playgroud)
对于淡入和淡出效果,我使用动画师和alpha值进行播放,例如:
[[self animator]setAlphaValue:0.9];
Run Code Online (Sandbox Code Playgroud)
编辑:这仅供参考,以便您可以采取一些想法
@interface MyButton : NSButton {
- (void)mouseEntered:(NSEvent *)theEvent;
- (void)mouseExited:(NSEvent *)theEvent;
- (void)updateTrackingAreas;
@end
Run Code Online (Sandbox Code Playgroud)