在我创建的应用程序中,当用户按下按钮时,我希望让某些NSOpenGLViews淡入淡出视图。为此,我使用NSViewAnimation创建了一个简短的测试应用程序,尝试在10秒内淡出视图。该代码紧密基于this post中的代码。
对于从NSView继承的常规对象,例如NSBox对象,代码工作得很好,但是当我尝试将其与NSOpenGLView对象一起使用时,视图在十秒钟内什么也不做,然后突然消失。为了让NSViewAnimation与NSOpenGLView协同工作,我还需要做些什么吗?或者在这种情况下,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发布于 2012-10-11 10:00:47
通过创建一个用于绘制OpenGL内容的CAOpenGLLayer子类,我设法达到了预期的结果。有关苹果的示例代码,请参阅here。淡入淡出是通过以下方式实现的:
- (IBAction)buttonPressed:(id)sender
{
static int isVisible = 1;
[theGLView.layer setHidden: isVisible];
isVisible = (isVisible + 1) % 2;
}发布于 2012-10-08 12:39:58
您可以尝试将NSOpenGLView指定为容器NSView的子视图,该容器的alphaValue是使用动画师设置的。
[[containerView animator] setAlphaValue: 0.0f];对我来说效果很好。
https://stackoverflow.com/questions/12775400
复制相似问题