Ios 如何使用CAKeyframeAnimation来实现反弹效果

Ios 如何使用CAKeyframeAnimation来实现反弹效果,ios,calayer,Ios,Calayer,我编写这段代码是为了创建CAKeyframeAnimation,但结果根本并没有反弹 -(CAKeyframeAnimation *)keyframeBounceAnimationFrom:(NSValue *)from to:(NSValue *)to forKeypath:(NSString *)keyPat

我编写这段代码是为了创建CAKeyframeAnimation,但结果根本并没有反弹

-(CAKeyframeAnimation *)keyframeBounceAnimationFrom:(NSValue *)from
                                                    to:(NSValue *)to
                                         forKeypath:(NSString *)keyPath
                                       withDuration:(CFTimeInterval)duration
{

    CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:keyPath];

    NSMutableArray * valuesArray = [NSMutableArray array];
    NSMutableArray * timeKeyArray = [NSMutableArray array];

    [self createBounceFrom:from to:to Values:valuesArray keyTimes:timeKeyArray];

    [animation setValues:valuesArray];
    [animation setKeyTimes:timeKeyArray];

    animation.duration = duration;
    return animation;
}

-(void)createBounceFrom:(NSValue *)from to:(NSValue *)to Values:(NSMutableArray *)values keyTimes:(NSMutableArray *)keyTimes
{

    CGPoint toPoint= [to CGPointValue];
    CGFloat offset = 60;
    CGFloat duration = 1.0f;
    NSUInteger numberOfOscillations= 4;


    [values addObject:from];
    [keyTimes addObject:[NSNumber numberWithFloat:0.0f]];
    //============
    //ideally bouncing will depend from starting position to end poisiton as simulating real Dumping Oscillations
    //============

    for (NSUInteger index= 0; index <numberOfOscillations ; index++)
    {


        CGPoint viaPoint = CGPointMake(toPoint.x,  toPoint.y + offset);
        NSValue * via = [NSValue valueWithCGPoint:viaPoint];
        [values addObject:via];
        //add time consumed for each oscillation
        [keyTimes addObject:[NSNumber numberWithFloat:duration]];
       // duration = duration - 0.1;

        offset = - offset ;
    }

    [values addObject:to];
    [keyTimes addObject:[NSNumber numberWithFloat:0.6]];

}
-(CAKeyframeAnimation*)keyframeBounceAnimationFrom:(NSValue*)from
至:(NSValue*)至
forKeypath:(NSString*)键路径
withDuration:(CFTimeInterval)持续时间
{
CAKeyframeAnimation*动画=[CAKeyframeAnimation动画WithKeyPath:keyPath];
NSMutableArray*值数组=[NSMutableArray];
NSMutableArray*timeKeyArray=[NSMutableArray];
[self-CreateBunceFrom:from-to:to-Values:ValuesRay-keyTimes:timeKeyArray];
[动画设置值:valuesArray];
[动画setKeyTimes:timeKeyArray];
animation.duration=持续时间;
返回动画;
}
-(void)createBounceFrom:(NSValue*)from to:(NSValue*)to Values:(NSMutableArray*)Values keyTimes:(NSMutableArray*)keyTimes
{
CGPoint toPoint=[到CGPointValue];
CGFloat偏移=60;
CGFloat持续时间=1.0f;
NSU整数值振荡=4;
[值addObject:from];
[keyTimes addObject:[NSNumber numberWithFloat:0.0f]];
//============
//理想情况下,反弹将取决于从起始位置到终点的泊松,就像模拟真实的倾卸振荡一样
//============

对于(nsuiger index=0;index,这里有一个很好的链接,其中包含一些如何使用它的示例:

(再看一看,我发现了这个:我认为它反弹得更好,但更高级。所以不要把代码放在这里)

CAKeyframeAnimation的示例如下:(取自链接)

-(void)animateGreenBall
{
    NSValue *from = @(greenBall.layer.position.y);
    CGFloat bounceDistance = 20.0;
    CGFloat direction = (greenUp? 1 : -1);

    NSValue *via = @((greenUp ? HEIGHT_DOWN : HEIGHT_UP) + direction*bounceDistance);
    NSValue *to = greenUp ? @(HEIGHT_DOWN) : @(HEIGHT_UP);
    NSString *keyPath = @"position.y";
    CAKeyframeAnimation *animation = [self keyframeBounceAnimationFrom:from
                                                                   via:via
                                                                    to:to
                                                            forKeyPath:keyPath
                                                          withDuration:.6];

    [greenBall.layer addAnimation:animation forKey:@"bounce"];
    [greenBall.layer setValue:to forKeyPath:keypath];
    greenUp = !greenUp;
}

-(CAKeyframeAnimation *)keyframeBounceAnimationFrom:(NSValue *)from 
                                                via:(NSValue *)via 
                                                 to:(NSValue *)to 
                                         forKeyPath:(NSString *)keyPath 
                                       withDuration:(CFTimeInterval)duration
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:keyPath];
    [animation setValues:@[from, via, to, nil]];
    [animation setKeyTimes:@[@(0), @(.7), @(1), nil]];

    animation.duration = duration;
    return animation;
}