我有 3 个动画在一组中运行:位置、缩放和旋转
但是我的旋转动画并不顺利。它在开始时突然发生,而其他动画则顺利发生。
这是我的代码:
//CABasicAnimation *scaleAnimation
//CABasicAnimation *moveAnimation
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
rotationAnimation.autoreverses = NO;
rotationAnimation.toValue = [NSNumber numberWithFloat: 0];//-(M_PI/3)];
//below line shows the end state, if removed, the layer will assume its initial position after animation, something which I don't want.
[layer setTransform:CATransform3DMakeRotation(0, 0, 1, 0)];
CAAnimationGroup *theGroup = [CAAnimationGroup animation];
//Code for adding all 3 animations to the group
编辑:
rotationAnimation.toValue = [NSNumber numberWithFloat: -(M_PI/3)];
从动画声明处移除最终变换值,并将其添加到委托。(请注意,在动画组的情况下,单个动画代表不会受到影响,只有组代表起作用)。
根据委托:
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
NSString * animName = [theAnimation valueForKey:@"animationName"];
CALayer* layer = [theAnimation valueForKey:@"animationLayer"];
if ([animName isEqualToString:@"MoveZoomOut"])
{
if ([layer.name isEqualToString:@"Layer1"])
[layer setTransform:CATransform3DMakeRotation(-M_PI/3, 0, 1, 0)];
else if ([layer.name isEqualToString:@"Layer2"])
[layer setTransform:CATransform3DMakeRotation(M_PI/3, 0, 1, 0)];
else
[layer setTransform:CATransform3DMakeRotation(0, 0, 1, 0)];
}
if ([layer respondsToSelector:@selector(setContentsScale:)])
{
layer.contentsScale = [UIScreen mainScreen].scale;
}
}