简体   繁体   English

为自定义CALayer属性设置动画

[英]animating a custom CALayer property

I'm trying to define an abstract layer property, angle, based on the existing layer property position. 我试图根据现有图层属性位置定义一个抽象图层属性angle。 Basically it describes the orientation of the layer from the center of a circle. 基本上,它从圆心描述了层的方向。 I did like the following: 我确实喜欢以下内容:

@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


It works fine when I manually set the value with setAngle:, but the problem is that when I try to animate it with a CABasicAnimation, the view sustained by the MenuItemLayer doesn't move at all and stays at its original position, while I can see that setValue: and drawInContext: get called normally along with the progress of the animation and so the position property of the presentation layer get updated. 当我使用setAngle:手动设置值时,它可以正常工作,但是问题是,当我尝试使用CABasicAnimation对其设置动画时,由MenuItemLayer维持的视图完全不会移动并保持其原始位置,而我可以看到setValue:和drawInContext:与动画的进度一起正常调用,因此表示层的position属性得到更新。

Anyone has some clues? 有人有线索吗? Thanks! 谢谢!



---- ----

Just noticed the following comment in the 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.


Does this mean that renderInContext: will always use model layer to render the image even in our case where the receiver of the drawInContext: method is a presentation layer? 这是否意味着即使在drawInContext:方法的接收者是表示层的情况下,renderInContext:仍将始终使用模型层来渲染图像? Could this be the cause of the problem? 这可能是问题的原因吗? If so, should I always manually translate the layer to the right location represented by the presentation layer? 如果是这样,我是否应该始终手动将图层平移到表示层所代表的正确位置?

What do you intend this to do? 您打算怎么办?

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

What happens when you comment it out? 当您注释掉该怎么办? I find that -renderInContext is most commonly used to render into a context for the sake of saving it as an image so this looks strange to me. 我发现-renderInContext最常用于渲染到上下文中,以将其保存为图像,因此这对我来说很奇怪。 A basic animation should be able to animate your property without a problem but overriding -drawInContext seems like it would only make sense if you're planning to do some custom drawing with CG. 基本的动画应该能够毫无问题地为您的属性设置动画,但是覆盖-drawInContext似乎只有在您打算使用CG进行自定义绘制时才有意义。

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

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