【问题标题】:NSCursor: "set" method has no effectNSCursor:“set”方法无效
【发布时间】:2011-12-08 11:23:39
【问题描述】:

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

【问题讨论】:

    标签: xcode macos cocos2d-iphone nscursor


    【解决方案1】:

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

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

    (斯威夫特 3)

    【讨论】:

      【解决方案2】:

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

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

      我是这样做的:

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

      我希望这能够帮助某人。

      【讨论】:

      • 嗯,这就是答案,我一点也不惊讶!我真的没有动力去尝试它,因为它是一些旧代码(我找到了另一种方法,即使用精灵而不是光标来做到这一点),但这似乎很合理,因为大多数图形函数都没有从事件调用时工作。因此,我会将其标记为答案,如果有机会尝试,我将对其进行编辑。
      • 这个doChangeCursor 似乎泄露了NSImageNSCursor 的实例。
      • JWWalker,你知道我们有 ARC 有一段时间了吗?
      • 与仅设置它相比,这很可靠。谢谢
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2011-10-20
        • 1970-01-01
        • 1970-01-01
        • 2021-09-20
        • 2012-04-01
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多