Gui*_*ume 15 iphone core-animation calayer
我正在尝试CALayer使用自定义index属性创建一个子类,我可以直接设置动画和更改,以便根据索引显示不同的图片.
在标题中,我声明:
@property NSUInteger index;
Run Code Online (Sandbox Code Playgroud)
在实施中,我覆盖了needDisplayForKey:
+ (BOOL)needsDisplayForKey:(NSString *)key
{
if ([key isEqualToString:@"index"])
return YES;
else
return [super needsDisplayForKey:key];
}
Run Code Online (Sandbox Code Playgroud)
现在,在该display方法中,我想基于索引显示图片.但是,在动画期间,值self.index永远不会更改,因此我必须查询表示层的值,这与通过子类提供CALayer内容的示例相反:
- (void)display
{
NSUInteger presentationIndex = [(CustomLayer *)[self presentationLayer] index];
self.contents = (id)[[images objectAtIndex:presentationIndex] CGImage];
}
Run Code Online (Sandbox Code Playgroud)
问题是,如果我这样做,我不能index直接在动画之外设置值,因为它只会更改模型层,并且该display方法显式查询表示层.
如果我添加一个复制值的初始化方法index,它的工作原理如下:
- (id)initWithLayer:(id)layer
{
self = [super initWithLayer:layer];
if (self) {
if ([layer isKindOfClass:[CustomLayer class]])
self.index = [(CustomLayer *)layer index];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
但是,在动画之后或之前,始终存在1个图像毛刺,因为演示或模型值不匹配(取决于我是否设置index为目标值).
drawInContext:方法始终具有正确的值[self index],但它不是我想要使用的方法,因为我只是content用图像设置属性.index属性的方式,我会得到不同的行为.如果我使用@dynamic index(虽然文档没有说动态实现自定义属性getter/setter),但display每次index更改值时都会调用它.如果我使用@synthesize或实现setter,display则不会调用,所以我也需要更改contentsetter.setValue:forKey:?正如您所看到的,我对如何获得我想要的结果以及如何使用自定义属性正确实现CALayer的子类感到困惑.任何帮助和解释,将不胜感激!
Dun*_*n C 11
Web上有一个很好的教程,解释了如何创建具有可动画属性的自定义CALayer子类.我没有机会尝试它,但我给它添加了书签.链接在这里:
看起来主要的技巧是:
使用@dynamic,而不是@synthesize来获取自定义CALayer的可动画属性.
覆盖actionForKey:,initWithLayer:和needsDisplayForKey:,也许还有drawInContext:
| 归档时间: |
|
| 查看次数: |
10179 次 |
| 最近记录: |