首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NSViewAnimation实现NSOpenGLViews淡入淡出

使用NSViewAnimation实现NSOpenGLViews淡入淡出
EN

Stack Overflow用户
提问于 2012-10-08 12:36:30
回答 2查看 270关注 0票数 1

在我创建的应用程序中,当用户按下按钮时,我希望让某些NSOpenGLViews淡入淡出视图。为此,我使用NSViewAnimation创建了一个简短的测试应用程序,尝试在10秒内淡出视图。该代码紧密基于this post中的代码。

对于从NSView继承的常规对象,例如NSBox对象,代码工作得很好,但是当我尝试将其与NSOpenGLView对象一起使用时,视图在十秒钟内什么也不做,然后突然消失。为了让NSViewAnimationNSOpenGLView协同工作,我还需要做些什么吗?或者在这种情况下,NSViewAnimation不是合适的工具吗?

相关代码:

代码语言:javascript
复制
// 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
EN

回答 2

Stack Overflow用户

发布于 2012-10-11 10:00:47

通过创建一个用于绘制OpenGL内容的CAOpenGLLayer子类,我设法达到了预期的结果。有关苹果的示例代码,请参阅here。淡入淡出是通过以下方式实现的:

代码语言:javascript
复制
- (IBAction)buttonPressed:(id)sender
{
    static int isVisible = 1;
    [theGLView.layer setHidden: isVisible];
    isVisible = (isVisible + 1) % 2;
}
票数 1
EN

Stack Overflow用户

发布于 2012-10-08 12:39:58

您可以尝试将NSOpenGLView指定为容器NSView的子视图,该容器的alphaValue是使用动画师设置的。

代码语言:javascript
复制
[[containerView animator] setAlphaValue: 0.0f];

对我来说效果很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12775400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档