如何每x秒调用一次方法?

时间:2011-02-26 04:36:10

标签: objective-c macos

我有一个NSWindow,我正在进入全屏模式。我希望能够在不使用时隐藏鼠标(比如说它最后一次使用后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

MyMediaRoomAppDelegate.m:

#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

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

1 个答案:

答案 0 :(得分:1)

试试NSTimer。您可以指定它在创建时重复。