多个CALayer动画同时执行

时间:2013-05-22 08:14:58

标签: ios core-animation calayer

我正在尝试制作一个动画,其中两个图层向侧面移动,缩小,并在3D中稍微旋转一下,所有这些都在同一时间(然后向前移动,而图层则先于底部不在顶部)。我尝试了几种方法,但似乎都没有。

我有像这样的3d变换动画:

perspectiveTransformLeft = CATransform3DIdentity;
perspectiveTransformLeft.m34 = 1.0 / 500;
perspectiveTransformLeft = CATransform3DRotate(perspectiveTransformLeft, 35.0f * M_PI / 360.0f, 0.0f, 1.0f, 0.0f);

我尝试添加无效的缩放变换:

perspectiveTransformLeft = CATransform3DMakeScale(0.75, 0.75, 1);

我试图在动画块中缩放图层,但这也不起作用:

[UIView animateWithDuration:1.0f
                      delay:0.0f
                    options: UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     endingLayer.frame = CGRectMake(20.0f, 0.0f, 724.0f, 538.0f);
                     switchViewBottom.layer.transform = perspectiveTransformRight;
                 }
                 completion:^(BOOL finished){
                     [delegate switchAnimationFinished];
                 }
 ];

我不知所措。有人能帮助我吗?

1 个答案:

答案 0 :(得分:5)

在CAAnimationGroup上做一些阅读并使用CABasicAnimations代替。 这应该让你实现你所追求的目标。如果您在实现它时遇到问题,我会在我的代码中搜索一个示例(我之前使用过它)。

编辑:这是一些代码

typedef void (^animationCompletionBlock)(void);
typedef void (^animationStartedBlock)(void);

- (void)addAnimations:(NSArray *)animations withDuration:(CGFloat)animationDuration onView:(UIView *)aView {
   animationStartedBlock startBlock = ^void(void) {
      // Additional Animation start code here
   };

   animationCompletionBlock endBlock = ^void(void) {
      // Additional animation completed code here
   };

   CAAnimationGroup *group = [CAAnimationGroup animation];
   group.fillMode = kCAFillModeForwards;
   group.removedOnCompletion = NO;
   group.duration = animationDuration;
   [group setAnimations:animations];
   group.delegate = self;

   [group setValue:startBlock forKey:@"animationStartedBlock"];
   [group setValue:endBlock forKey:@"animationCompletionBlock"];

   [aView.layer addAnimation:group forKey:@"yourAnimationName"];
}

这将在你的代表中调用你的完成块

// Animation Delegate
- (void)animationDidStart:(CAAnimation *)anim {
   animationStartedBlock animationStartedBlock = [anim valueForKey:@"animationStartedBlock"];
   if (animationStartedBlock) {
      animationStartedBlock();
   }
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
   animationCompletionBlock animationCompleteBlock = [theAnimation valueForKey:@"animationCompletionBlock"];
   if (animationCompleteBlock) {
      animationCompleteBlock();
   }
}

如何创建动画并将它们添加到数组以传递给此方法由您决定,具体取决于您想要的动画。

这是两个缩放/淡入淡出动画的示例:

// Scale
- (CABasicAnimation *)scaleAnimationForImageView:(UIImageView *)imageView withDuration:(CGFloat)duration {
   CGRect imageFrame = imageView.frame;
   CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
   [resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(40.0f, imageFrame.size.height * (40.0f / imageFrame.size.width))]];
   resizeAnimation.fillMode = kCAFillModeForwards;
   resizeAnimation.duration = duration;
   resizeAnimation.removedOnCompletion = NO;
   return resizeAnimation;
}

// Fade
- (CABasicAnimation *)fadeAnimationWithFinalOpacity:(CGFloat)opacity withDuration:(CGFloat)duration {
   CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
   [fadeOutAnimation setToValue:[NSNumber numberWithFloat:opacity]];
   fadeOutAnimation.fillMode = kCAFillModeForwards;
   fadeOutAnimation.removedOnCompletion = NO;
   fadeOutAnimation.duration = duration;
   return fadeOutAnimation;
}