2012-10-08 37 views
1

在我創建的應用程序中,我希望當用戶按下按鈕時,某些NSOpenGLViews淡入和淡出視圖。爲此,我創建了一個使用NSViewAnimation的簡短測試應用程序,嘗試在10秒內淡出視圖。該代碼基於this post使用NSViewAnimation使NSOpenGLView淡入淡出

的代碼完全適用於從NSView繼承,例如NSBox對象一般對象,但是當我試圖用NSOpenGLView對象使用它,認爲什麼都不做10秒鐘,然後突然消失。有什麼額外的工作要做NSViewAnimationNSOpenGLView,或NSViewAnimation不適合在這種情況下工作的正確工具嗎?

相關的代碼:

// AppDelegate.m 
#import "AppDelegate.h" 

@implementation AppDelegate 
@synthesize theForeground; // an instance of a the Foreground class - a subclass of NSOpenGLView 
@synthesize theBox; 
@synthesize theBackground; 

//code omitted 

- (IBAction)buttonPressed:(id)sender 
{ 
    NSViewAnimation *theAnim; 
    NSMutableDictionary * theViewDict; 

    theViewDict = [NSMutableDictionary dictionaryWithCapacity:2]; 
    [theViewDict setObject: theForeground forKey:NSViewAnimationTargetKey]; 
    [theViewDict setObject:NSViewAnimationFadeOutEffect 
        forKey:NSViewAnimationEffectKey]; 

    theAnim = [[NSViewAnimation alloc] initWithViewAnimations: [NSArrayarrayWithObject:theViewDict]]; 

    [theAnim setDuration:10.0]; 
    [theAnim setAnimationCurve:NSAnimationEaseInOut]; 

    [theAnim startAnimation]; 

    [theAnim release]; 
} 
@end 


// Foreground.m 

#import "ForegroundView.h" 

@implementation ForegroundView 

// code omitted 
- (void)drawRect:(NSRect)dirtyRect 
{ 
    glClearColor(0, 0, 0, 1.0); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glColor3f(1.0, 0.0, 0.0); 
    glBegin(GL_QUADS); 
     glVertex2f(-0.5, -0.5); 
     glVertex2f(0.5, -0.5); 
     glVertex2f(0.5, 0.5); 
     glVertex2f(-0.5, 0.5); 
    glEnd(); 
    glFlush(); 
} 

@end 

回答

0

你可以嘗試分配NSOpenGLView作爲容器的NSView其alphaValue你的動畫集的子視圖。

[[containerView animator] setAlphaValue: 0.0f]; 

它適用於我。

+0

我試過這個建議,但事情與原始方法相同。它可以與'NSBox','NSButton'等對象一起使用,但不能與'NSOpenGLView'對象一起使用。 – Andrew

1

我設法通過使CAOpenGLLayer子類繪製OpenGL內容來達到預期的效果。 Apple示例代碼請參見here。通過以下步驟實現淡入淡出:

- (IBAction)buttonPressed:(id)sender 
{ 
    static int isVisible = 1; 
    [theGLView.layer setHidden: isVisible]; 
    isVisible = (isVisible + 1) % 2; 
}