button.h:
#import <AppKit/AppKit.h>@interface button : NSbutton {}- (voID)mouseEntered:(NSEvent *)theEvent;- (voID)mouseExited:(NSEvent *)theEvent;- (voID)mouseDown:(NSEvent *)ev;- (voID)mouseUp:(NSEvent *)theEvent;@end
button.m:
#import“button.h”
@implementation button- (ID)initWithFrame:(NSRect)frameRect { self = [super initWithFrame:frameRect]; if(self != nil) { NSLog(@"btn init");} return self;}- (voID)mouseEntered:(NSEvent *)theEvent{ NSLog(@"mouseEntered"); [self setimage:[NSImage imagenamed:@"lockIcon_2.png"]]; [self setNeedsdisplay];}- (voID)mouseExited:(NSEvent *)theEvent{ [self setimage:[NSImage imagenamed:@"lockIcon_1.png"]]; NSLog(@"mouseExited"); [self setNeedsdisplay];}- (voID)mouseDown:(NSEvent *)ev { NSLog(@"mouseDown!");}- (voID)mouseUp:(NSEvent *)ev { NSLog(@"mouseUp!");}@end
有了上面的代码,每次点击一个按钮,我都会在日志中看到“mouseDown”,但是我看不到“mouseEntered”和“mouseExited”(当然看不到图像的变化)?可悲的是,我知道我错过了一些明显的东西,但我只是没有看到它?
解决方法 问题是NSbutton只有在您将自定义NSTrackingArea添加到按钮时才能处理某些鼠标事件.尝试在你的按钮类中添加这个代码.它帮了我如果他们不满意你也可以选择选项.
- (voID)createTrackingArea{ NSTrackingAreaOptions focusTrackingAreaOptions = NSTrackingActiveInActiveApp; focusTrackingAreaOptions |= NSTrackingMouseEnteredAndExited; focusTrackingAreaOptions |= NSTrackingAssumeInsIDe; focusTrackingAreaOptions |= NSTrackingInVisibleRect; NSTrackingArea *focusTrackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:focusTrackingAreaOptions owner:self userInfo:nil]; [self addTrackingArea:focusTrackingArea];}- (voID)awakeFromNib{ [self createTrackingArea];}
希望它有帮助.
总结以上是内存溢出为你收集整理的objective-c – 可可按钮滚动与mouseEntered:和mouseExited:?全部内容,希望文章能够帮你解决objective-c – 可可按钮滚动与mouseEntered:和mouseExited:?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)