2010-07-03 212 views
2

我想根据现有图层属性位置定义抽象图层属性角度。基本上它描述了从一个圆的中心的层的方向。我不喜欢以下内容:动画自定义CALayer属性

@interface MenuItemLayer : CALayer 
    @property CGFloat angle; 
@end 

@implementation MenuItemLayer 

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

- (void)drawInContext: (CGContextRef)context { 
    [self renderInContext: context]; 
} 

- (CGFloat)angle { 
    CGPoint center = self.superlayer.center; 
    CGPoint pos = self.position; 
    return atan2f(pos.x - center.x, center.y - pos.y); 
} 

- (void)setAngle: (CGFloat)angle { 
    CGPoint center = self.superlayer.center; 
    CGFloat radius = 100; 
    [CATransaction begin]; 
    [CATransaction setDisableActions: YES]; 
    self.position = CGPointMake(center.x + radius * sinf(angle), 
           center.y - radius * cosf(angle)); 
    [CATransaction commit]; 
} 

@end 


它工作正常,当我手动设置与setAngle值:,但问题是,当我试图用一个CABasicAnimation进行动画处理它,由MenuItemLayer持续的观点没有按根本不会移动并保持其原始位置,而我可以看到setValue:和drawInContext:会随着动画的进度而正常调用,因此表示层的position属性会更新。

任何人都有一些线索?谢谢!



----

只注意到在doc以下注释:

 
renderInContext:
This method renders directly from the layer tree, ignoring any animations added to the render tree. Renders in the coordinate space of the layer.


这是否意味着renderInContext:将始终使用模型层呈现图像,即使在我们的情况下,drawInContext:方法的接收者是表示层?这可能是问题的原因吗?如果是这样,我应该总是手动将图层转换到表示层所代表的正确位置?

回答

0

你打算怎么做?

- (void)drawInContext: (CGContextRef)context { 
    [self renderInContext: context]; 
} 

当你评论它时会发生什么?我发现-renderInContext最常用于渲染到上下文中,以便将它保存为图像,所以这对我来说看起来很奇怪。一个基本的动画应该能够动画你的属性没有问题,但覆盖-drawInContext似乎只有在你打算用CG做一些自定义绘图时才有意义。

+0

感谢您的回答。如果我删除了drawInContext:这是很自然的,因为我使用needsDisplayForKey机制来通知CA框架我的图层应该被重画。我打算renderInContext只是用动画属性值重绘图层。 – Kay 2010-07-04 07:05:18

+0

你能否更新你的答案并向我展示你的CABasicAnimation代码?您是否期望您对角度定制属性进行更改以使位置动画?这有两个问题。一个是你已经禁用了这些动作,所以改变位置不会动画。其次是我相信如果你想动画两个属性(一个自定义,角度和一个位置),你使用两个独立的动画和一个动画组。除了我想你想让你的位置根据角度的改变来改变和设置动画,对吧? – 2010-07-05 16:58:30