1

我正在尝试为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);

提前致谢。

4

1 回答 1

4

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

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

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

于 2012-06-20T03:06:05.153 回答