Objective c 如何每x秒调用一个方法?

Objective c 如何每x秒调用一个方法?,objective-c,macos,Objective C,Macos,我有一个全屏模式的窗口。我想能够隐藏鼠标时,它不在使用中(比如说15秒后,它是最后一次使用)。我的申请代表如下: MyMediaRoomAppDelegate.h: #import <Cocoa/Cocoa.h> @interface MyMediaRoomAppDelegate : NSResponder <NSApplicationDelegate> { NSWindow *window; NSDate *lastMouseMove; } @pro

我有一个全屏模式的窗口。我想能够隐藏鼠标时,它不在使用中(比如说15秒后,它是最后一次使用)。我的申请代表如下:

MyMediaRoomAppDelegate.h:

#import <Cocoa/Cocoa.h>

@interface MyMediaRoomAppDelegate : NSResponder <NSApplicationDelegate> {
    NSWindow *window;
    NSDate *lastMouseMove;
}

@property (assign) IBOutlet NSWindow *window;
@property (nonatomic, retain) NSDate *lastMouseMove;

@end
我不确定的是如何在15秒后重新隐藏光标。问题是我需要每隔一秒左右检查setLastMouseMove,而不是在15秒后调用
[NSCursor hide]

试试看。您可以将其指定为在创建时重复

#import "MyMediaRoomAppDelegate.h"

@implementation MyMediaRoomAppDelegate

@synthesize window;
@synthesize lastMouseMove;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // The application has just finished lanching

    // Grab the screen size
    NSRect screenRect;
    screenRect = [[NSScreen mainScreen] frame];

    // Setup the window - full screen
    [[self window] setLevel:NSMainMenuWindowLevel+1];
    [[self window] setStyleMask:NSBorderlessWindowMask];
    [[self window] setOpaque:YES];
    [[self window] setBackgroundColor:[NSColor blackColor]];
    [[self window] setFrame:screenRect display:YES animate:NO];

    // Setup the mouse
    [[self window] setAcceptsMouseMovedEvents:YES];
    [[self window] makeFirstResponder:self];
    [NSCursor hide];
}

- (BOOL)acceptsFirstResponder
{
    return YES;
}

- (void)mouseMoved:(NSEvent *)theEvent
{
    [NSCursor unhide];
    [self setLastMouseMove: [NSDate date]];
}

@end