1

我正在处理基于FlyingImages的核心动画过渡。

我希望能够在过渡演变时将不透明度设置为 0 到 1,并在反转时设置回零。这可能吗,以及如何..

这是我目前所在的位置:

- (CATransform3D)originTransformForLayer:(CALayer *)layer; {
  CGPoint layerPosition = [layer position];
  CATransform3D originTransform = CATransform3DMakeTranslation(20.f - layerPosition.x, -layerPosition.y, 0.f);
  return originTransform;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  [[self durationStepper] setIncrement:.05f];
  [[self durationStepper] setDoubleValue:.4f];
  shouldReverse_ = NO;

  //Start with all of the images at the origin
  [CATransaction begin];
  [CATransaction setDisableActions:YES];

  for (CALayer *imageLayer in [[[self imageContainer] layer] sublayers]) {
      [imageLayer setOpacity:0];
    [imageLayer setTransform:[self originTransformForLayer:imageLayer]];
      // NSLog(@"tag: %@", [[[self imageContainer] layer] sublayers] );
  }
  [CATransaction commit];
}

- (IBAction)runAnimation:(id)sender {

    CALayer *containerLayer = [[self imageContainer] layer];

  NSTimeInterval delay = 0.2f;
  NSTimeInterval delayStep = .05f;
  NSTimeInterval singleDuration = [[self durationStepper] doubleValue];
  NSTimeInterval fullDuration = singleDuration + (delayStep * [[containerLayer sublayers] count]);

  NSEnumerator *imageLayerEnumerator;
   if(shouldReverse_) {
      newTransform = [self originTransformForLayer:imageLayer];
        [imageLayer setOpacity:0];
    } else {
        [imageLayer setOpacity:1];
      newTransform = CATransform3DIdentity;
    }

  for (CALayer *imageLayer in imageLayerEnumerator) {
    CATransform3D currentTransform = [[imageLayer presentationLayer] transform];
    CATransform3D newTransform;

    if(shouldReverse_) {
      newTransform = [self originTransformForLayer:imageLayer];

    } else {

      newTransform = CATransform3DIdentity;
    }

    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
    anim.beginTime = delay;
    anim.fromValue = [NSValue valueWithCATransform3D:currentTransform];
    anim.toValue = [NSValue valueWithCATransform3D:newTransform];
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    anim.fillMode = kCAFillModeBackwards;
    anim.duration = singleDuration;

    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = [NSArray arrayWithObject:anim];
    group.duration = fullDuration;

    [imageLayer setTransform:newTransform];

    [imageLayer addAnimation:group forKey:@"transform"];
    delay += delayStep;
  }
  shouldReverse_ = !shouldReverse_;
}

但这是一个黑客,我不喜欢它..

4

0 回答 0