iOS 让物体进行曲线运动

来源:互联网 发布:win6网络异常怎么修复 编辑:程序博客网 时间:2024/06/05 14:25


iOS 开发中有时候需要对某些物体进行简单的动画处理

比如frame变大变小

,或者是位置改变

目前的位置改变动画,其中有些需要实现曲线运动。


曲线运动该使用什么样的方法呢?

答案是 layer的postion动画可以实现。


一个简单的曲线运动的动画可以这么写:

        CGMutablePathRef path = CGPathCreateMutable();        CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y);        CGPathAddQuadCurveToPoint(path, NULL, controlPoint.x , controlPoint.y, endPoint.x, endPoint.y);        CAKeyframeAnimation *animate = [CAKeyframeAnimation animationWithKeyPath:@"position"];        animate.delegate =self;        animate.duration = 1.5;        animate.fillMode = kCAFillModeForwards;        animate.repeatCount = 0;        animate.path = path;        animate.removedOnCompletion = NO;        CGPathRelease(path);        [self.panda.layer addAnimation:animate forKey:@"jakillTest"];

上述可以实现一个 物体的 曲线运动动画。原理就是使用到了 CGMutablePathRef

向其中添加一些路径,

动画开始时讲路径赋值给 layer层便可。


详细的代码见demo

demo

0 0
原创粉丝点击