REPRO存储库在这里:https://github.com/kaolin/38492498/tree/master
我有一个@property int percentScanned.
我设置了needsDisplayForKey:
+ (BOOL)needsDisplayForKey:(NSString *)key {
if ([@"percentScanned" isEqualToString:key]) {
return YES;
}
return [super needsDisplayForKey:key];
}
Run Code Online (Sandbox Code Playgroud)
我的显示方法:
- (void)display {
_percentTextLayer.string = [NSString stringWithFormat:@"%02d%%",[[self presentationLayer] percentScanned]];
}
Run Code Online (Sandbox Code Playgroud)
我的动画:
[CATransaction begin];
self.percentScanned = 99;
_percentTextLayer.string=@"99%";
[CATransaction setCompletionBlock:^{
_percentTextLayer.string=@"99%";
self.percentScanned = 99;
}];
{
CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"percentScanned"];
[a setFromValue:@0];
[a setToValue:@99];
[a setDuration:4.];
[self addAnimation:a forKey:@"percentage"];
}
[CATransaction commit];
Run Code Online (Sandbox Code Playgroud)
这一切都很好,除了在动画结束时,我的模型层字符串返回到"00%".正如你所看到的,我已经抛出,如厨房水槽迫使它留在"99%"什么感觉(外的"黑客"的animation.removedOnCompletion = NO;和animation.fillMode = kCAFillModeForwards;-and实际上,添加这些并不能帮助!).
我做有关于一秒钟后3/30在同一层上另一个动画,淡入淡出的不透明度....但推说出来,或者删除它,似乎不有所作为.
我知道如何为标准动画值执行此操作,但是我错过了如何使用像这样的辅助/评估值来执行此操作....
编辑添加:
某处一路上,增加animation.removedOnCompletion = NO;并animation.fillMode = kCAFillModeForwards;没有真正开始"工作",但我想不会泄露动画....
如果我为percentScanned添加一个setter,那么最后不会调用0,而是在结尾调用display 0.跟踪调用的setPercentScanned和被调用的显示之间的链接很难...
我可以修改display如下:
- (void)display {
if ([[self presentationLayer] percentScanned] != 0) {
_percentTextLayer.string = [NSString stringWithFormat:@"%02d%%",[[self presentationLayer] percentScanned]];
}
}
Run Code Online (Sandbox Code Playgroud)
然后它适用于我的特定用例,但这仍然不太理想....
在显示方法中放置一个断点是相当无益的(当然它只是在display_if_needed中发生).:/