我有一个想要在屏幕上设置动画的 CALayer。我创建了两种方法:一种滑动打开图层,另一种滑动关闭。这些都通过将属性分配给图层的变换属性来工作。
现在我想使用 CAKeyFrameAnimation 来滑动打开图层。我得到了这个工作,所以图层滑动打开,但现在我不能使用我的旧方法滑动图层关闭。我试图弄清楚为什么会这样。任何帮助都会很棒。
我的 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:openTransform],nil];
closeAnimation.timingFunctions = [NSArray arrayWithObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[self addAnimation:closeAnimation forKey:@"transform"];
bIsOpen = !bIsOpen;
}
}