5

我正在尝试制作一个动画,其中两个图层同时移动到侧面,缩小并在 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];
                 }
 ];

我很茫然。有人能帮我吗?

4

1 回答 1

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;
}
于 2013-05-22T08:35:15.890 回答