gpt4 book ai didi

objective-c - 使用 NSViewAnimation 使 NSOpenGLView 淡入淡出

转载 作者:行者123 更新时间:2023-12-03 17:13:57 30 4
gpt4 key购买 nike

在我正在创建的应用程序中,我希望当用户按下按钮时某些 NSOpenGLViews 淡入和淡出 View 。为此,我使用 NSViewAnimation 创建了一个简短的测试应用程序,尝试在十秒的时间内淡出 View 。该代码与 this post 中的代码密切相关。 。

该代码非常适合从 NSView 继承的一般对象,例如 NSBox 对象,但是当我尝试将它与 NSOpenGLView 一起使用时> 对象, View 十秒内没有任何反应,然后突然消失。我是否需要做一些额外的事情才能让 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

最佳答案

我通过创建一个 CAOpenGLLayer 子类来绘制 OpenGL 内容,从而达到了预期的结果。请参阅here苹果示例代码。然后通过以下方式实现淡入和淡出:

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

关于objective-c - 使用 NSViewAnimation 使 NSOpenGLView 淡入淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12775400/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com