最近在看iOS的核心动画(Core Animation)里面有2个方法:
- (CFTimeInterval)convertTime:(CFTimeInterval)t fromLayer:(CALayer *)l;
- (CFTimeInterval)convertTime:(CFTimeInterval)t toLayer:(CALayer *)l;
官方文档解释不是很清楚,自己研究了一下,并做下记录。
1. - (CFTimeInterval)convertTime:(CFTimeInterval)t toLayer:(CALayer *)l;
1. toLayer不为nil时
用例1:
CALayer *layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
CALayer *layer1 = [[CALayer alloc] init];
[self.view.layer addSublayer:layer1];
layer1.timeOffset = 1;
layer1.speed = 0.3;
NSLog(@"%f",[layer convertTime:0.5 toLayer:layer1]);
结果打印为:1.15,很容易得出是1+0.3*0.5得到的
用例2:
CALayer *layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
CALayer *layer1 = [[CALayer alloc] init];
[self.view.layer addSublayer:layer1];
layer.timeOffset = 1;
layer1.timeOffset = 1;
layer1.speed = 0.3;
NSLog(@"%f",[layer convertTime:0.5 toLayer:layer1]);
结果打印为:0,为什么将layer的timeOffset设置成1以后,结果变成0了呢?
用例3:
CALayer *layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
CALayer *layer1 = [[CALayer alloc] init];
[self.view.layer addSublayer:layer1];
layer.timeOffset = 0.8;
layer1.timeOffset = 1;
layer1.speed = 0.3;
NSLog(@"%f",[layer convertTime:0.5 toLayer:layer1]);
结果仍然是:0,我觉得将layer的timeOffset设置更小试一下
用例4:
CALayer *layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
CALayer *layer1 = [[CALayer alloc] init];
[self.view.layer addSublayer:layer1];
layer.timeOffset = 0.2;
layer1.timeOffset = 1;
layer1.speed = 0.3;
NSLog(@"%f",[layer convertTime:0.5 toLayer:layer1]);
结果为:1.09,终于不是0了,可以看出是1+(0.5 - 0.2)* 0.3得到的,经过多次测试发现,只有当layer的timeOffset小于等于convertTime时,值才不为0。
用例5:
CALayer *layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
CALayer *layer1 = [[CALayer alloc] init];
[self.view.layer addSublayer:layer1];
layer.timeOffset = 0.2;
layer.speed = 2;
layer1.timeOffset = 1;
layer1.speed = 0.3;
NSLog(@"%f",[layer convertTime:0.5 toLayer:layer1]);
结果为:1.045,可以看出,把speed增大,结果反而变小了:1 + (0.5 - 0.2) * 0.3 / 2
所以结果为: layer1.timeOffset + (convertTime - layer.timeOffset) * layer1.speed / layer.speed
2. toLayer为nil时
用例1.
CALayer *layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
CALayer *layer1 = [[CALayer alloc] init];
[self.view.layer addSublayer:layer1];
layer.speed = 4;
NSLog(@"%f",[layer convertTime:0.8 toLayer:nil]);
结果为0.2,可以得出是0.8 / 4
用例2.
CALayer *layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
CALayer *layer1 = [[CALayer alloc] init];
[self.view.layer addSublayer:layer1];
layer.speed = 4;
layer.timeOffset = 1;
NSLog(@"%f",[layer convertTime:0.8 toLayer:nil]);
结果为- 0.05,可以得出时(0.8 - 1) / 4
所以结果为(convertTime - timeOffset) / speed
2.- (CFTimeInterval)convertTime:(CFTimeInterval)t fromLayer:(CALayer *)l;
1.fromLayer不为nil时
用例1:
CALayer *layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
CALayer *layer1 = [[CALayer alloc] init];
[self.view.layer addSublayer:layer1];
layer1.timeOffset = 1;
layer1.speed = 0.3;
NSLog(@"%f",[layer convertTime:0.5 fromLayer:layer1]);
打印结果:-1.666667,没明白是咋回事,换个speed试试
用例2:
CALayer *layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
CALayer *layer1 = [[CALayer alloc] init];
[self.view.layer addSublayer:layer1];
layer1.timeOffset = 1;
layer1.speed = 0.2;
NSLog(@"%f",[layer convertTime:0.5 fromLayer:layer1]);
打印结果:-2.5,可以得出是- (1 / 0.2) * 0.5得到的
用例3:
CALayer *layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
CALayer *layer1 = [[CALayer alloc] init];
[self.view.layer addSublayer:layer1];
layer1.timeOffset = 1;
layer1.speed = 0.2;
NSLog(@"%f",[layer convertTime:0.4 fromLayer:layer1]);
打印结果:-3,咦,按照上面不应该-(1 / 0.2)* 0.4 = 2吗?猜测是不是- (1 / 0.2)* (1 - 0.4) = -3
用例4:
CALayer *layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
CALayer *layer1 = [[CALayer alloc] init];
[self.view.layer addSublayer:layer1];
layer1.timeOffset = 1;
layer1.speed = 0.2;
NSLog(@"%f",[layer convertTime:0.8 fromLayer:layer1]);
打印结果:-1,符合上面的猜测
经测试如果layer设置speed和timeOffset之后结果就为0,原因暂时不得而知。
所以结果为: - (layer1.timeOffset / layer1.speed) * (layer1.timeOffset - convertTime)
2.fromLayer为nil时
用例1.
CALayer *layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
CALayer *layer1 = [[CALayer alloc] init];
[self.view.layer addSublayer:layer1];
layer.speed = 5;
NSLog(@"%f",[layer convertTime:0.8 fromLayer:nil]);
结果为4,很好得出5 * 0.8
用例2.
CALayer *layer = [[CALayer alloc] init];
[self.view.layer addSublayer:layer];
CALayer *layer1 = [[CALayer alloc] init];
[self.view.layer addSublayer:layer1];
layer.speed = 5;
layer.timeOffset = 2;
NSLog(@"%f",[layer convertTime:0.8 fromLayer:nil]);
结果为6,很好得出5 * 0.8 + 2