NSCursor:& quot; set&"方法无效 [英] NSCursor: "set" method has no effect
问题描述
我正在使用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"方法,然后它可以正常工作,并显示正确的光标.
我尝试了很多东西,弹出和推入方法,"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.
由于某些原因,从应用程序事件中调用[cursor set]无效.
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.
这篇关于NSCursor:& quot; set&"方法无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!