NSCursor:“设置”方法没有效果
我正在使用 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”方法,然后它可以正常工作,显示正确的光标。
我已经尝试了很多东西,弹出和推送方法,“setOnMouseEnter”(虽然我还没有使用矩形),但没有结果......
有人对此有任何线索吗?
编辑:
陌生人,我写了 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 中的通知并在代码中的其他地方调用它,那么它不会执行任何操作(尽管我检查它是否被调用)...
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...
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];
}
In the initialization:
[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateCursor:) userInfo:nil repeats:NO];
And the method:
-(void) updateCursor:(id)sender
{
CCLOG(@"Update cursor");
[[self.cursorsDict objectForKey:@"point"] set];
}
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?
Edit:
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];
}
Which is called by a notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWake) name:EVENT_APPLICATION_DID_BECOME_ACTIVE object:nil];
Set in the appDelegate by:
-(void) applicationDidBecomeActive:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:EVENT_APPLICATION_DID_BECOME_ACTIVE object:nil];
}
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)...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我从系统事件更改光标的工作解决方案是将光标集包装在异步内,如下所示:
(Swift 3)
Working solution for me to change the cursor from system event was to wrap the cursor set inside async like so:
(Swift 3)
我知道已经有一段时间了。但我也有类似的问题。
由于某种原因,从应用程序事件调用时 [cursor set] 不起作用。
我是这样做的:
我希望这能够帮助别人。
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.
Here is how I did it:
I hope this is able to help someone.
好吧,我找不到解决这个问题的办法。我必须使用解决方法:处理设置在光标位置的精灵光标...
Well, I could not find any solution to this problem. I had to use a workaround: handle a sprite cursor set at the position of the cursor...