我已经成功地动画了我的图层的位置和变换(旋转)。
现在,我需要为anchorPoint设置动画,但在所有尝试中都失败了!即使我这样做:
2012... App[6437:403] layer: 1, KeyPath: anchorPoint, fromValue: NSPoint: {1.05, 0.5}, toValue: NSPoint: {1.05, 0.5}, Duration: 2.5
这是在动画图层之前卡住的NSLog ..我得到的结果是在X轴上向左移动的图层太远,然后又回来了......并重复(因为自动反转是ON)。
关于图层层次结构的注意事项: SuperLayer有三个子层,我正在应用动画。
为什么呢?锚点应该如何动画?
我的代码:
NSArray* subKeyPath = [keyPaths objectAtIndex:tag];
NSArray* subToValue = [toValues objectAtIndex:tag];
NSArray* subDuration = [durations objectAtIndex:tag];
NSEnumerator* keyPathsEnum = [subKeyPath objectEnumerator];
NSEnumerator* toValuesEnum = [subToValue objectEnumerator];
NSEnumerator* durationsEnum = [subDuration objectEnumerator];
NSString* keyPath;
NSNumber* toValue;
NSNumber* duration;
while ((keyPath = [keyPathsEnum nextObject]) && (toValue = [toValuesEnum nextObject]) && (duration = [durationsEnum nextObject])) {
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:keyPath];
id offsetToValue;
if ([keyPath hasPrefix:@"position"]) {
//I removed the position animation, since animating the anchorPoint should suffice
} else if ([keyPath hasPrefix:@"anchorPoint"]) {
//This was when I used anchorPoint.x float value = [[subLayer valueForKeyPath:keyPath] floatValue] - ([toValue floatValue]/[[subLayer contents] size].width);
offsetToValue = [NSNumber valueWithPoint:CGPointMake(1.05, 0.5)];
keyPath = [keyPath substringToIndex:[keyPath length]-2];
} else {
offsetToValue = toValue;
}
...
animation.toValue = offsetToValue;
animation.duration = [duration floatValue];
animation.fillMode = kCAFillModeForwards;
animation.repeatCount = INFINITY;
animation.autoreverses = YES;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[subLayer addAnimation:animation forKey:keyPath];
//update the model
}
我尝试动画“anchorPoint.x”keyPath,结果相同:(
最后请注意,我正在编写一个可可应用程序,但在可可和可可触摸上设置X值的动画应该完全相同。
答案 0 :(得分:2)
你不会相信这一点(好像我从一开始就和你坐在一起)..
在不设置fromValue
的情况下,动画显示位置和旋转正常。但是,设置锚点动画需要我将锚点fromValue
设置为初始值!
animation.fromValue = [subLayer valueForKeyPath:keyPath];