CAKeyframeAnimation 关键路径错误

Posted

技术标签:

【中文标题】CAKeyframeAnimation 关键路径错误【英文标题】:CAKeyframeAnimation Keypath Error 【发布时间】:2012-06-20 02:56:52 【问题描述】:

我正在尝试为视图的transform 属性设置动画。这是我的代码:

CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
NSArray *transformValues = [NSArray arrayWithObjects:
                        [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation((3*M_PI)/8)],
                        [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(-((3*M_PI)/8))],
                        [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation((3*M_PI)/8)],
                        [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(-((3*M_PI)/8))],
                        [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation((3*M_PI)/8)],
                        [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(-((3*M_PI)/8))],
                        nil];

[shakeAnimation setValues:transformValues];

NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.16f],[NSNumber numberWithFloat:0.33f],[NSNumber numberWithFloat:0.49f],[NSNumber numberWithFloat:0.66f],[NSNumber numberWithFloat:0.82f], [NSNumber numberWithFloat:1.0f], nil];

[shakeAnimation setKeyTimes:times];

NSArray *timingFunctions = [NSArray arrayWithObjects:
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], 
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], 
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], 
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], 
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], 
nil];

[shakeAnimation setTimingFunctions:timingFunctions];

shakeAnimation.fillMode = kCAFillModeForwards;
shakeAnimation.removedOnCompletion = NO;
shakeAnimation.duration = 1.0f;

[self.someView.layer addAnimation:shakeAnimation forKey:@"anim"];

由于某种原因,每当我将方法animationWithKeyPath: 的参数设置为“转换”时,都会出现错误。我已经尝试了半个小时来调试这个,但找不到错误。我最初虽然我在 UIView 的转换属性上执行了不允许的动画,但我知道我可以做这样的事情:

aView.transform = CGAffineTransformMakeRotation(M_PI/2);

提前致谢。

【问题讨论】:

【参考方案1】:

图层的transform 属性的值是CATransform3D,而不是CGAffineTransform

此外,您可能会发现为 transform.rotation.z 键路径设置动画更简单。该值只是一个CGFloat,因此您根本不必创建变换矩阵。

“Modifying a Transform Using Key Paths” in Core Animation Programming Guide“Key Path Support for Structure Fields” in Core Animation Programming Guide

【讨论】:

你是对的!我在考虑视图的transform 属性。非常感谢。此外,transform.rotation.z 键路径采用的 CGFloat 值代表什么?角度(如果是弧度或度数)? 没关系。我检查了你的链接。它是以弧度为单位的旋转角度。

以上是关于CAKeyframeAnimation 关键路径错误的主要内容,如果未能解决你的问题,请参考以下文章

ios之CAKeyframeAnimation关键帧动画详解

1.0 秒后的 CAKeyframeAnimation 关键时间未执行

使用 CGPathAddArcToPoint 构建路径的 CAKeyframeAnimation 不起作用

iOS CoreAnimation 关键帧动画 CAKeyframeAnimation

核心动画(CAKeyframeAnimation,CABasicAnimation)

具有交错开始时间的 CAKeyframeAnimation