我正在使用Xcode(使用cocos2d)开发Mac应用程序,尝试配置光标,而set"方法在某些情况下似乎没有任何效果......
I am working on a Mac application with Xcode (using cocos2d), trying to configure the cursor, and the "set" method does not seem to have any effect in some cases...
我已经很难在应用程序启动时设置光标(NSTimer 在应用程序真正启动后设置它),现在我只希望它在用户点击时显示另一个图像;我为此使用 NSNotification,我的光标类收到通知,然后它应该设置新图像,然后......什么都没有.
I already had a hard time to set my cursor at the launch of the application (NSTimer to set it after the application was really launched), and now I just want it to display another image when the user clicks; I use NSNotification for that, my cursor class receives the notification, then it is supposed to set the new image, then... nothing.
这里有一些可能有帮助的代码:
Here is some code which may help:
-(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),但没有结果...有人知道吗?
The "updateCursor" method is also called when the application becomes active, and then it works fine, the correct cursor is displayed. I have tried many things, pop and push methods, "setOnMouseEnter" (although I don't use rect yet), but no result... Has anyone a clue about this?
陌生人,我写了appWake方法:
Stranger, I wrote the appWake method:
-(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中设置:
Set in the appDelegate by:
-(void) applicationDidBecomeActive:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:EVENT_APPLICATION_DID_BECOME_ACTIVE object:nil];
}
当它被这个通知调用时,它工作正常,光标随机变化;但是如果我删除 applicationDidBecomeActive 中的通知并在我的代码中的其他地方调用它,那么它不会做任何事情(尽管我检查了它是否被调用)......
And when it's called by this notification, it works fine, the cursor changes randomly; but if I remove the notification in applicationDidBecomeActive and call it somewhere else in my code, then it does not do anything (although I checked that it is called)...
我知道已经有一段时间了.但我也有类似的问题.
I know it has been awhile. But I had a similar problem.
由于某种原因,从应用程序事件调用时 [光标集] 不起作用.
For some reason [cursor set] doesn't work when being called from the application events.
我是这样做的:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self performSelector:@selector(doChangeCursor) withObject:nil afterDelay:1];
}
- (void) doChangeCursor
{
NSString *file = [[NSBundle mainBundle] pathForResource:@"statusBarImage" ofType:@"tiff"];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:file];
NSCursor *cursor = [[NSCursor alloc] initWithImage:image hotSpot:NSMakePoint(0, 0)];
[cursor set];
}
我希望这能够帮助某人.
I hope this is able to help someone.