繁体   English   中英

为自定义CALayer属性设置动画

[英]animating a custom CALayer property

我试图根据现有图层属性位置定义一个抽象图层属性angle。 基本上,它从圆心描述了层的方向。 我确实喜欢以下内容:

@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属性得到更新。

有人有线索吗? 谢谢!



----

刚注意到文档中的以下注释:

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.


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

您打算怎么办?

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

当您注释掉该怎么办? 我发现-renderInContext最常用于渲染到上下文中,以将其保存为图像,因此这对我来说很奇怪。 基本的动画应该能够毫无问题地为您的属性设置动画,但是覆盖-drawInContext似乎只有在您打算使用CG进行自定义绘制时才有意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM