Ios 向翻转动画添加反弹效果
我为我的饼图制作了翻转动画。这是我的密码“Ios 向翻转动画添加反弹效果,ios,objective-c,animation,cabasicanimation,Ios,Objective C,Animation,Cabasicanimation,我为我的饼图制作了翻转动画。这是我的密码“ animationDidStop:finished: CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"]; animation.fromValue = [NSNumber numberWithFloat:0.5]; animation.toValue = [NSNumber numberWithFloat:1.0]; ani
animationDidStop:finished:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
animation.fromValue = [NSNumber numberWithFloat:0.5];
animation.toValue = [NSNumber numberWithFloat:1.0];
animation.duration = 1.0f;
[self.pieChart addAnimation:animation forKey:@"scale"];
如何将反弹效果添加到第二个动画中?(
animationDidStop:finished:
)使用CAKeyFrameAnimation
而不是两个CABasicAnimation
:
CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
scaleAnimation.keyTimes = @[@(0), @(0.5), @(0.9), @(1)];
scaleAnimation.values = @[@(1.0), @(0.5), @(1.1), @(1.0)];
scaleAnimation.duration = 1.0f;
scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
使用keyTimes
和values
,您可以在动画中指定多个姿态。在这里,我让它以1的比例开始,动画比例为0.5(您的第一个动画值),然后是1.1(创建“反弹”的超调),最后是1(您的第二个动画值)。根据您的喜好调整它们
如果要分两部分执行此动画,可以保留第一个动画,然后在animationDidStop:finished:
中启动CAKeyFrameAnimation
,类似于此:
CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
scaleAnimation.keyTimes = @[@(0), @(0.8), @(1)];
scaleAnimation.values = @[@(0.5), @(1.1), @(1.0)];
scaleAnimation.duration = 1.0f;
scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
如果改为使用UIView动画块,则需要执行以下类似操作:
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.squareView.transform = CGAffineTransformMakeScale(0.5, 1);
} completion:^(BOOL finished) {
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:0 animations:^{
self.squareView.transform = CGAffineTransformIdentity;
} completion:nil];
}];
您可能需要使用参数来调整动画。谢谢!!是否可以在
[UIView animateWithDuration…]
中执行此操作?@Jessica我编辑了我的答案,以提供一个备选方案,使用两个CAAnimation
,另一个使用UIView
动画块(您需要两个)。
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.squareView.transform = CGAffineTransformMakeScale(0.5, 1);
} completion:^(BOOL finished) {
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:0 animations:^{
self.squareView.transform = CGAffineTransformIdentity;
} completion:nil];
}];