我想同时影响图层变换的缩放和平移,但只运行平移动画。这是错误的代码...
在这里,我使用 scale.y 的 keypath 创建缩放动画:
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 值的字符串,我猜这与我正在为变换的两个方面设置动画这一事实有关,但我认为使用该组可以解决这个问题。唉...