我想同时影响图层变换的比例和平移,但只运行翻译动画。这是错误的代码......
在这里,我使用keypath开始缩放动画.y:
self.mainImage.layer.anchorPoint = CGPointMake(0.5, 1);
CAKeyframeAnimation *mainAnimationScale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
mainAnimationScale.duration = 0.75;
mainAnimationScale.repeatCount = 0;
mainAnimationScale.removedOnCompletion = YES;
mainAnimationScale.autoreverses = NO;
mainAnimationScale.fillMode = kCAFillModeForwards;
mainAnimationScale.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:0.7],
[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:0.85],
[NSNumber numberWithFloat:1.0],
nil];
mainAnimationScale.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0],
[NSNumber numberWithFloat:0.65],
[NSNumber numberWithFloat:0.72],
[NSNumber numberWithFloat:0.84],
[NSNumber numberWithFloat:0.91],
[NSNumber numberWithFloat:1],
nil];
在这里,我创建了包含translation.y:
的关键路径的翻译动画 CAKeyframeAnimation *mainAnimationTrans = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
mainAnimationTrans.duration = 0.75;
mainAnimationTrans.repeatCount = 0;
mainAnimationTrans.removedOnCompletion = YES;
mainAnimationTrans.autoreverses = NO;
mainAnimationTrans.fillMode = kCAFillModeForwards;
mainAnimationTrans.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:-250.0],
[NSNumber numberWithFloat:-250.0],
[NSNumber numberWithFloat:0],
nil];
mainAnimationTrans.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0],
[NSNumber numberWithFloat:0.65],
[NSNumber numberWithFloat:1],
nil];
在这里,我将它们组合在一起并将它们应用到UIView的图层:
CAAnimationGroup *theGroup = [CAAnimationGroup animation];
theGroup.duration = 0.75;
theGroup.repeatCount = 0;
theGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
theGroup.animations = [NSArray arrayWithObjects:mainAnimationTrans, mainAnimationScale, nil];
[self.mainImage.layer addAnimation:theGroup forKey:@"layerAnimation"];
我最初尝试将动画直接添加到这样的图层,但这也不起作用:
[self.mainImage.layer addAnimation:mainAnimationTrans forKey:@"transform.scale.y"];
[self.mainImage.layer addAnimation:mainAnimationTrans forKey:@"transform.translation.y"];
我已经尝试了我能为keyPath值考虑的每一个字符串,并且我猜它与我正在动画变换的两个方面这一事实有关,但我认为使用该组会修复那。唉...
答案 0 :(得分:2)
使这项工作的关键是结合变换。我不知道为什么我没有意识到这一点,因为它是基本的矩阵数学......我想我的同事昨天甚至提到过它。的Ack。
无论如何,我想要的动画是让物品下降并在到达目的地后反弹。这是代码:
self.mainImage.layer.anchorPoint = CGPointMake(0.3, 1.0);
CAKeyframeAnimation *mainImageAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
mainImageAnimation.duration = 0.75;
mainImageAnimation.repeatCount = 0;
mainImageAnimation.removedOnCompletion = NO;
mainImageAnimation.autoreverses = NO;
mainImageAnimation.fillMode = kCAFillModeForwards;
mainImageAnimation.values = [NSArray arrayWithObjects:
[NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DMakeTranslation(-8, -250, 0), 1, 1, 1)],
[NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DMakeTranslation(-8, 17, 0), 1, 1, 1)],
[NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DMakeTranslation(-8, 17, 0), 1.2, .8, 1)],
[NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DMakeTranslation(-8, 17, 0), .85, 1.15, 1)],
[NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DMakeTranslation(-8, 17, 0), 1.1, .9, 1)],
[NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DMakeTranslation(-8, 17, 0), 1, 1, 1)],
nil];
mainImageAnimation.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0],
[NSNumber numberWithFloat:0.6],
[NSNumber numberWithFloat:0.65],
[NSNumber numberWithFloat:0.78],
[NSNumber numberWithFloat:0.88],
[NSNumber numberWithFloat:1],
nil];
mainImageAnimation.timingFunctions = [NSArray arrayWithObjects:
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
nil];
[self.mainImage.layer addAnimation:mainImageAnimation forKey:@"transform"];
注意事项:
希望这有帮助!
答案 1 :(得分:0)
与问题不同,但相关,我试图结合两个CAKeyFrameAnimations,旋转和缩放。
CAKeyframeAnimation *wiggle = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
wiggle.values = @[ @0.0, @-0.15, @0.0, @0.15, @-0.15, @0.0];
wiggle.keyTimes = @[ @0.0, @0.2, @0.4, @0.6, @0.8, @1.0];
wiggle.duration = 0.5;
CAKeyframeAnimation *resize = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
resize.duration = 0.5;
resize.values = @[@1.0, @1.5, @1.0];
resize.keyTimes = @[@0.0, @0.5, @1.0];
单独添加动画不起作用,因为仅轮换发生。
[self.layer addAnimation:wiggle forKey:kAnimationKey];
[self.layer addAnimation:resize forKey:kAnimationKey];
但当我创建一个包含CAKeyFrameAnimations的动画组时,它有效。
CAAnimationGroup *animGroup = [[CAAnimationGroup alloc] init];
animGroup.animations = @[wiggle, resize];
animGroup.duration = 0.5;
[self.layer addAnimation:animGroup forKey:kAnimationKey];