2011-11-30 31 views
1

我試圖在Xcode中創建一個屏幕保護程序部署爲.saver文件。可可屏幕嵌入石英

但是,我想嵌入一個Quartz Composition文件(QTZ)。

由於屏幕保護程序模板中沒有XIB或NIB,因此如何在代碼中嵌入qtz?

這裏是什麼在ScreenSaverView.h:

#import <ScreenSaver/ScreenSaver.h> 
@interface XmasView : ScreenSaverView 
@end 

而且在ScreenSaverView.m,

#import "ScreenSaverView.h" 

@implementation XmasView 

- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview 
{ 
self = [super initWithFrame:frame isPreview:isPreview]; 
if (self) { 
    [self setAnimationTimeInterval:1/30.0]; 
} 
return self; 
} 

- (void)startAnimation 
{ 
    [super startAnimation]; 
} 

- (void)stopAnimation 
{ 
    [super stopAnimation]; 
} 

- (void)drawRect:(NSRect)rect 
{ 
    [super drawRect:rect]; 
} 

- (void)animateOneFrame 
{ 
    return; 
} 

- (BOOL)hasConfigureSheet 
{ 
    return NO; 
} 

- (NSWindow*)configureSheet 
{ 
    return nil; 
} 

@end 

我相信我已經把在initWithFrame一些代碼:嵌入石英組成?如果是這樣,我需要輸入什麼?

在此先感謝

回答

2

你只需要創建一個QCView實例,並把它作爲您的屏幕視圖的子視圖:

.H

#import <ScreenSaver/ScreenSaver.h> 
#import <Quartz/Quartz.h> 

@interface XmasView : ScreenSaverView 
@property (strong) QCView* qtzView; 
@end 

.M

#import "ScreenSaverView.h" 

@implementation XmasView 
@synthesize qtzView; 

- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview 
{ 
    self = [super initWithFrame:frame isPreview:isPreview]; 
    if (self) 
    { 
     [self setAnimationTimeInterval:1/30.0]; 

     //create the quartz composition view 
     qtzView = [[QCView alloc] initWithFrame: NSMakeRect(0, 0, NSWidth(frame), NSHeight(frame))]; 
     //make sure it resizes with the screensaver view 
     [qtzView setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)]; 

     //match its frame rate to your screensaver 
     [qtzView setMaxRenderingFrameRate:30.0f]; 

     //get the location of the quartz composition from the bundle 
     NSString* compositionPath = [[NSBundle mainBundle] pathForResource:@"YourComposition" ofType:@"qtz"]; 
     //load the composition 
     [qtzView loadCompositionFromFile:compositionPath]; 

     //add the quartz composition view 
     [self addSubview:qtzView]; 
    } 
    return self; 
} 

//...implementation continues 
+0

完美。謝謝:D – Josh

+0

這不適用於Xcode 6.4。子視圖被創建,但構圖從不渲染器。 我考慮使用QCRenderer來顯式渲染組合,但放棄了:https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/QuartzFramework/Classes/QCRenderer_Class/ –