因此,我们遇到了一些有趣的问题,似乎是表示层的缓存,我们想知道其他人是否看到了这一点,以及我们可以采取哪些解决方法。
我们有几个我们自己的属性,我们正在隐式动画。我们通过将它们声明为属性并动态合成它们来做到这一点:
@interface GOOLayer : CALayer
@property float gooValue;
@end
NSString *const kGOOLayerValueKey = @"gooValue";
@implementation GOOLayer
@dynamic gooValue;
- (id)initWithLayer:(id)layer {
if ((self = [super initWithLayer:layer])) {
id value = [layer valueForKey:kGOOLayerValueKey];
[self setValue:value forKey:kGOOLayerValueKey];
}
return self;
}
- (id<CAAction>)actionForKey:(NSString *)event {
id <CAAction> action = [super actionForKey:event];
if (action == nil && [event isEqual:kGOOLayerValueKey]) {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:event];
animation.fromValue = [self.presentationLayer valueForKey:event];
animation.duration = [CATransaction animationDuration];
return animation;
}
return action;
}
- (void)display {
GOOLayer *presoLayer = [self presentationLayer];
NSLog(@"Display GooValue: %f", presoLayer.gooValue);
}
+ (BOOL)needsDisplayForKey:(NSString *)key {
if ([key isEqual:kGOOLayerValueKey]) {
return YES;
}
return [super needsDisplayForKey:key];
}
在大多数情况下,这可以正常工作,并且我们会看到记录了适当的值。在第一种情况下,我们得到了适当的隐式动画:
- (IBAction)doSomeGoo:(id)sender {
sublayer_.gooValue = random();
}
输出:
2012-12-19 10:10:41.440 CoreAnimation[94149:c07] Display GooValue: 1804289408.000000
2012-12-19 10:10:41.502 CoreAnimation[94149:c07] Display GooValue: 1804289408.000000
...
2012-12-19 10:10:41.739 CoreAnimation[94149:c07] Display GooValue: 898766464.000000
2012-12-19 10:10:41.757 CoreAnimation[94149:c07] Display GooValue: 846930880.000000
在第二种情况下,我们禁用这些操作,这样我们就可以得到一个单一的值更新,这很好:
- (IBAction)doSomeGoo2:(id)sender {
long newValue = random();
NSLog(@"newValue = %f", (float)newValue);
[CATransaction begin];
[CATransaction setDisableActions:YES];
sublayer_.gooValue = newValue;
[CATransaction commit];
}
第一次通过:
2012-12-19 10:11:16.208 CoreAnimation[94149:c07] newValue = 1714636928.000000
2012-12-19 10:11:16.209 CoreAnimation[94149:c07] Display GooValue: 1714636928.000000
第二次通过:
2012-12-19 10:11:26.258 CoreAnimation[94149:c07] newValue = 1957747840.000000
2012-12-19 10:11:26.259 CoreAnimation[94149:c07] Display GooValue: 1957747840.000000
这是我们在执行有趣的事务之前调用 [CALayerpresentationLayer] 的第三种情况:
- (IBAction)doSomeGoo3:(id)sender {
GOOLayer *presoLayer = sublayer_.presentationLayer;
long newValue = random();
NSLog(@"presoLayer.gooValue = %f newValue = %f", presoLayer.gooValue, (float)newValue);
[CATransaction begin];
[CATransaction setDisableActions:YES];
sublayer_.gooValue = newValue;
[CATransaction commit];
}
第一次通过:
2012-12-19 10:12:12.505 CoreAnimation[94149:c07] presoLayer.gooValue = 1957747840.000000 newValue = 424238336.000000
2012-12-19 10:12:12.505 CoreAnimation[94149:c07] Display GooValue: 1957747840.000000
第二次通过:
2012-12-19 10:12:13.905 CoreAnimation[94149:c07] presoLayer.gooValue = 424238336.000000 newValue = 719885376.000000
2012-12-19 10:12:13.906 CoreAnimation[94149:c07] Display GooValue: 424238336.000000
请注意添加presentationLayer 调用如何使显示无法获得当前值。
关于如何处理这个问题的任何建议,或者我是否完全误解了事情,这是预期的行为?