为自定义CALayer属性设置动画 [英] animating a custom CALayer property

查看:71
本文介绍了为自定义CALayer属性设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to define an abstract layer property, angle, based on the existing layer property position. 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



当我使用setAngle:手动设置值时,它可以很好地工作,但是问题是,当我尝试使用CABasicAnimation对其设置动画时,由MenuItemLayer维持的视图完全不动,并保持其原始位置,而当我可以看到setValue:和drawInContext:随动画进度正常调用,因此表示层的position属性也得到了更新。


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


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.

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.



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


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? 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?

推荐答案

您打算如何做?

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

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

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. 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.

这篇关于为自定义CALayer属性设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆