-(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/我觉得它反d得更好,但更高级.所以不要把代码放在这里)
http://www.touchwonders.com/some-techniques-for-bouncing-animations-in-ios/
CAKeyframeAnimation的示例如下:(取自链接)
-(voID)animateGreenBall{ NSValue * from = [NSNumber numberWithfloat:greenBall.layer.position.y]; CGfloat bouncedistance = 20.0; CGfloat direction = (greenUp? 1 : -1); NSValue * via = [NSNumber numberWithfloat:(greenUp ? HEIGHT_DOWN : HEIGHT_UP) + direction*bouncedistance]; NSValue * to = greenUp ? [NSNumber numberWithfloat:HEIGHT_DOWN] : [NSNumber numberWithfloat:HEIGHT_UP]; Nsstring * keypath = @"position.y"; [greenBall.layer addAnimation:[self keyframeBounceAnimationFrom:from via:via to:to forKeypath:keypath withDuration:.6] 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:[NSArray arrayWithObjects:from,via,to,nil]]; [animation setKeyTimes:[NSArray arrayWithObjects:[NSNumber numberWithfloat:0.0],[NSNumber numberWithfloat:.7],[NSNumber numberWithfloat:1.0],nil]]; animation.duration = duration; return animation;}总结
以上是内存溢出为你收集整理的如何使用CAKeyframeAnimation用于Bouncing effect IOS全部内容,希望文章能够帮你解决如何使用CAKeyframeAnimation用于Bouncing effect IOS所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)