gpt4 book ai didi

iphone - 使用 CABasicAnimation 时,CALayer 未绘制其内容

转载 作者:太空狗 更新时间:2023-10-30 03:55:04 28 4
gpt4 key购买 nike

我有一个带有自定义 CALayer 子类的应用程序。在这一层子类中,我已经覆盖了 - (void) drawInContext:(CGContextRef)ctx 方法。这工作正常,我所有的自定义内容都被绘制出来了。

现在我想添加一个自定义属性,当它的内容发生变化时,它会变成动画。我需要在动画运行时重绘。我添加了以下问题的答案中给出的属性:Animating a custom property of CALayer subclass

而且我还在我的层中为此属性添加了一个 CABasicAnimation 实例:

CABasicAnimation* theAnimation=[CABasicAnimation animationWithKeyPath:@"moveContentToLeft"];
theAnimation.duration=1.0;
theAnimation.fromValue=[NSNumber numberWithFloat:0.0];
theAnimation.toValue=[NSNumber numberWithFloat:10.0];
[area addAnimation:theAnimation forKey:@"moveContentToLeft"];

在我的绘制方法中,我有一个 NSLog 语句,我在其中输出我正在设置动画的属性的值。

我的问题是,一旦动画开始,图层子类的所有内容都会被清除。 NSLog 输出被插值的我的属性的值(因此 drawInContext 方法)被调用。但不知何故,它绘制的东西在动画过程中是不可见的。在动画结束时,原始内容再次可见。

目前在绘制图层时尚未使用动画属性,因此我希望正常内容会再次绘制,并且我会得到带有插值的 NSLog 的输出。

我的层是另一个层(在 UIView 内)的子层。我试图在绘图方法的末尾重绘 super 层(调用 _parent setNeedsDisplay [parent is a ivar to the parent view])。这没有帮助。而且我也觉得这样做不是一个好主意。

我希望有人能帮我解决这个问题。

更新

感谢 Matt Long 的示例项目,我可以看出我的问题出在绘图方法中。我扩展了他的项目,所以它显示了同样的问题。我认为它比原始代码更容易展示 ;)

- (void)drawInContext:(CGContextRef)ctx
{
NSLog(@"Getting called: Bob: %i", [self bob]);

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(50 + [self bob], 50, 100, 100));
CGContextAddPath(ctx, path);
CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextSetLineWidth(ctx, 1.0);
CGContextStrokePath(ctx);
CGPathRelease(path);

[_test testInContext:ctx]; // here is the problem
}

ivar _test 是一个自定义类,其中包含一些绘图代码(目前它绘制了一个绿色框)。现在的问题是动画运行时不会绘制这个绿色框。动画结束后,绿色框再次可见。扩展示例项目:http://dl.dropbox.com/u/5426092/ArbitraryPropertyAnimationNew.zip

最佳答案

在没有看到更多代码的情况下很难说出了什么问题。我有一个示例应用程序,它同时为自定义属性和层的位置设置动画。你可以take a look at my source看看有什么不同。

关于iphone - 使用 CABasicAnimation 时,CALayer 未绘制其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3360672/

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