CAKeyframeAnimation键盘错误

时间:2012-06-20 02:56:52

标签: objective-c ios uiview core-animation cakeyframeanimation

我正在尝试为视图的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:的参数设置为“transform”时,我都会收到错误。我已经尝试了半个小时来调试这个,但找不到错误。我最初虽然在UIView的转换属性上执行动画是不允许的,但我知道我可以做这样的事情:

aView.transform = CGAffineTransformMakeRotation(M_PI/2);

先谢谢。

1 个答案:

答案 0 :(得分:4)

图层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