开发者

Issue with CAAnimation and CALayer Transforms

I have a CALayer that I want to animate across the screen. I have created two methods: one slide open the layer and one to slide close. These both work by assigning a property to the layer's transform property.

Now I want to use a CAKeyFrameAnimation to slide open the layer. I got this working so the layer slides open, but now I can't slide the layer close using my old method. I am trying to figure out why this is. Any help would be great.

Code for my CALayer:

- (id)init
{
  if( self = [super init] )
  {
    bIsOpen = NO;
    closeTransform = self.transform;
    openTransform = CATransform3DMakeTranslation(-235.0, 0.0, 0.0);
  }
  return self;
}

- (void)closeMenu
{
  if( bIsOpen )
  {
    self.transform = closeTransform;
    bIsOpen = !bIsOpen;
  }  
}

- (void)openMenu
{
  if( !bIsOpen )
  {
    CAKeyframeAnimation *closeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    closeAnimation.duration = 1.0;
    closeAnimation.removedOnCompletion = NO;
    closeAnimation.fillMode = kCAFillModeForwards;
    closeAnimation.values = [NSArray arrayWithObjects:[NSValue valueWithCATransform3D:closeTransform],[NSValue valueWithCATransform3D:openTra开发者_StackOverflow社区nsform],nil];
    closeAnimation.timingFunctions = [NSArray arrayWithObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];

    [self addAnimation:closeAnimation forKey:@"transform"];
    bIsOpen = !bIsOpen;
  }
}


I figured this out. My problem was that the animation animates the object as if it modifying the property that is changed ( in this case the transform ), but the value isn't actually changed. I believe this has to do with the Presentation Layer vs. the Model Layer. So when the animation was done for opening the layer, the layer was positioned on the screen correctly, but its transform has not actually changed. So to fix this I used the (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag delegate method to set the transform to the opened state once the animation was complete.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜