2

我正在为 Mac OS X 10.6+ 开发 Mac 应用程序,并且需要在动画发生时重绘 CAOpenGLLayer 的内容。我想我已经阅读了所有必要的部分,但这对我不起作用。我设置动画如下:

[CATransaction setAnimationDuration:SLIDE_DURATION];
CABasicAnimation *drawAnim = [CABasicAnimation animationWithKeyPath:@"drawIt"];
[drawAnim setFromValue:[NSNumber numberWithFloat:0]];
[drawAnim setToValue:[NSNumber numberWithFloat:1]];
[drawAnim setDuration:SLIDE_DURATION];
[chartView.chartViewLayer addAnimation:drawAnim forKey:@"drawIt"];

chartFrame.origin.x += controlFrame.size.width;
chartFrame.size.width -= controlFrame.size.width;
chartView.chartViewLayer.frame = CGRectMake(chartFrame.origin.x, 
                                            chartFrame.origin.y, 
                                            chartFrame.size.width, 
                                            chartFrame.size.height);

drawIt 属性是一个自定义属性,其唯一目的是在连续动画帧期间调用它来绘制图层。为了让它工作,你必须将它添加到 chartViewLayer 的类中:

+ (BOOL)needsDisplayForKey:(NSString *)key 
{
    if ([key isEqualToString:@"drawIt"]) 
    {
        return YES;
    } 
    else 
    {
        return [super needsDisplayForKey:key];
    }
}

所以这似乎一切正常。但是,我需要在绘制图层之前获取图层的当前(动画)大小。我发现了有关如何从表示层中获取此信息的各种相互矛盾的信息。这是我在图层时尝试过的:

drawInCGLContext:(CGLContextObj)glContext 
     pixelFormat:(CGLPixelFormatObj)pixelFormat 
    forLayerTime:(CFTimeInterval)timeInterval 
     displayTime:(const CVTimeStamp *)timeStamp

在动画期间调用方法。我尝试使用 KVC 并通过查询框架或边界来获取大小。

CALayer *presentationLayer = [chartViewLayer presentationLayer];
//bounds.size.width = [[[chartViewLayer presentationLayer]
//                        valueForKeyPath:@"frame.size.width"] intValue];
//bounds.size.height = [[[chartViewLayer presentationLayer]
//                        valueForKeyPath:@"frame.size.height"] intValue];
//bounds.size = presentationLayer.bounds.size;

bounds.size = presentationLayer.frame.size;
NSLog(@"Size during animation: %f, %f", bounds.size.width, bounds.size.height);

在所有情况下,返回值都是动画的最终结果,而不是过渡值。我的理解是使用presentationLayer应该给出过渡值。

那么这只是坏了还是我错过了一些关键步骤?谢谢你的帮助。

4

0 回答 0