3

我正在使用 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 中的通知并在我的代码中的其他地方调用它,那么它不会做任何事情(尽管我检查它是否被调用)......

4

3 回答 3

3

我从系统事件更改光标的工作解决方案是将光标集包装在 async 中,如下所示:

DispatchQueue.main.async {
    self.customCursor.set()
}

(斯威夫特 3)

于 2016-10-06T20:23:00.823 回答
1

我知道已经有一段时间了。但我有一个类似的问题。

由于某种原因,从应用程序事件调用时 [cursor set] 不起作用。

这是我的做法:

- (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];    
}

我希望这能够帮助某人。

于 2012-12-12T20:41:03.333 回答
0

好吧,我找不到任何解决这个问题的方法。我不得不使用一种解决方法:处理设置在光标位置的精灵光标......

于 2011-12-16T10:41:36.243 回答