gpt4 book ai didi

ios - 如何使用 CAKeyframeAnimation 用于弹跳效果 IOS

转载 作者:可可西里 更新时间:2023-11-01 05:45:44 25 4
gpt4 key购买 nike

我编写了这段代码来创建 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]];

}

最佳答案

这是一个很好的链接,其中包含一些如何使用它的示例:

(经过更多查看,我发现了这个:http://www.cocoanetics.com/2012/06/lets-bounce/,我认为它的弹跳要好得多,但更先进。所以不会把代码放在这里)

http://www.touchwonders.com/some-techniques-for-bouncing-animations-in-ios/

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;
}

关于ios - 如何使用 CAKeyframeAnimation 用于弹跳效果 IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14445229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com