我正在尝试为视图的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);
先谢谢。
答案 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