Cocoa下實現SplashScreen

#import <Cocoa/Cocoa.h>
@interface SplashWindow : NSWindow
- (id)initWithSplashImage:(NSString*)imgfile;
@end


//SplashWindow.m

#import "SplashWindow.h"


@implementation SplashWindow

- (id)initWithSplashImage:(NSString*)imgfile
{
	NSRect screenRect = [[NSScreen mainScreen] frame]; // NSRect for screen
	
	NSImage *backgroundImage = [NSImage imageNamed:imgfile];
	NSSize size = [backgroundImage size];
	CGRect contentRect = CGRectMake(screenRect.size.width /2-size.width/2, screenRect.size.height/2-size.height/2, size.width, size.height);
	[self setBackgroundColor:[NSColor colorWithPatternImage:backgroundImage]];
	self = [super initWithContentRect:contentRect
							styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered
								defer:NO];
	[self orderFront:self ];
	return self;
}

- (BOOL)acceptsMouseMovedEvents
{
	return NO;
}
@end


//AppDelegate.h

#import <Cocoa/Cocoa.h>
#import "SplashWindow.h"

@interface AppDelegate : NSObject {

   IBOutlet SplashWindow* _splashWindow;
  
}

@end

//AppDelegate.m

#import "AppDelegate.h"


@implementation AppDelegate

- (void)applicationWillFinishLaunching:(NSNotification *)aNotification 
{  
	_splashWindow = [[SplashWindow alloc]initWithSplashImage:@"bg_splash"];
}

- (void)dealloc
{
	[_splashWindow release];
	[super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  
  // hide the about box after one second.
  [NSTimer scheduledTimerWithTimeInterval: 5.0f 
                                   target:self 
                                 selector:@selector(closeSplashBox:) 
                                 userInfo:self 
                                  repeats:false];
}

- (void)closeSplashBox:(NSTimer*)theTimer
{
	[_splashWindow close];
}

@end





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章