我已经成功地动画了我的图层的位置和变换(旋转)。动画CALayer anchorPoint
现在,我需要对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轴waaaay太远的图层,然后回来之前..和重复(因为自动重新打开)。
关于图层层次结构的注意事项: 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值应为正是在两个可可和可可触摸你是不是要去一样。
如果我可能会对您的代码本身发表评论,它很好,很干净 –
谢谢@JamesGraham!我主要与那些在编码时看不到这种艺术价值的开发者合作:/ – Mazyod
阅读:)是一种快乐:) –