我正在使用 Xcode(使用 cocos2d)开发 Mac 应用程序,尝试配置光标,而“set”方法在某些情况下似乎没有任何效果......
我已经很难在应用程序启动时设置光标(NSTimer 在应用程序真正启动后设置它),现在我只希望它在用户单击时显示另一个图像;我为此使用 NSNotification,我的光标类收到通知,然后它应该设置新图像,然后......什么都没有。
以下是一些可能有帮助的代码:
-(void) click
{
CCLOG(@"Click");
NSString *path = [[NSBundle mainBundle] pathForResource:@"point_pressed" ofType:@"png"];
NSImage *cursorImage = [[[NSImage alloc] initByReferencingFile:path] autorelease];
NSCursor *cursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:[[NSCursor currentCursor] hotSpot]];
[cursor set];
}
在初始化中:
[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateCursor:) userInfo:nil repeats:NO];
和方法:
-(void) updateCursor:(id)sender
{
CCLOG(@"Update cursor");
[[self.cursorsDict objectForKey:@"point"] set];
}
当应用程序变为活动状态时,也会调用“updateCursor”方法,然后它工作正常,显示正确的光标。
我已经尝试了很多东西,pop 和 push 方法,“setOnMouseEnter”(虽然我还没有使用 rect),但没有结果......
有人知道这个吗?
编辑:
陌生人,我写了appWake方法:
-(void) appWake
{
int i = rand()%3;
if(i==0)
[[self.cursorsDict objectForKey:@"point"] set];
else if(i==1)
[[self.cursorsDict objectForKey:@"point_pressed"] set];
else if(i==2)
[[self.cursorsDict objectForKey:@"open"] set];
self.state = ECursorState_Point;
[NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(appWake) userInfo:nil repeats:NO];
}
由通知调用:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWake) name:EVENT_APPLICATION_DID_BECOME_ACTIVE object:nil];
通过以下方式在 appDelegate 中设置:
-(void) applicationDidBecomeActive:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:EVENT_APPLICATION_DID_BECOME_ACTIVE object:nil];
}
当它被这个通知调用时,它工作正常,光标随机变化;但是如果我删除 applicationDidBecomeActive 中的通知并在我的代码中的其他地方调用它,那么它不会做任何事情(尽管我检查它是否被调用)......