您的位置:首页 > 移动开发 > IOS开发

iOS做旋转动画的几种方法

2016-02-03 11:36 525 查看
iOS中可能会用到一些旋转动画的效果,这部分动画其实主要使用的还是CoreAnimation进行,实现起来有好几种方法,但最终的效果都是一样的

上代码

第一种:使用CABasicAnimated方法

这种方法是最简单的方法

CABasicAnimation *animation =  [CABasicAnimation animationWithKeyPath:@“transform.rotation.z"];

//默认是顺时针效果,若将fromValue和toValue的值互换,则为逆时针效果

animation.fromValue = [NSNumbernumberWithFloat:0.f];

animation.toValue =  [NSNumbernumberWithFloat: M_PI *2];

animation.duration  = 3;

animation.autoreverses = NO;

animation.fillMode =kCAFillModeForwards;

animation.repeatCount = MAXFLOAT; //如果这里想设置成一直自旋转,可以设置为MAXFLOAT,否则设置具体的数值则代表执行多少次

[view.layer addAnimation:animation forKey:nil];

第二种:使用CGPath绘制路线执行
这种方法用到了CoreGraphics库中的CGPathAddArc方法

CGMutablePathRef path = CGPathCreateMutable();

//CGPathAddArc函数是通过圆心和半径定义一个圆,然后通过两个弧度确定一个弧线。注意弧度是以当前坐标环境的X轴开始的。

//需要注意的是由于iOS中的坐标体系是和Quartz坐标体系中Y轴相反的,所以iOS UIView在做Quartz绘图时,Y轴已经做了Scale为-1的转换,因此造成CGPathAddArc函数最后一个是否是顺时针的参数结果正好是相反的,也就是说如果设置最后的参数为1,根据参数定义应该是顺时针的,但实际绘图结果会是逆时针的!

//严格的说,这个方法只是确定一个中心点后,以某个长度作为半径,以确定的角度和顺逆时针而进行旋转,半径最低设置为1,设置为0则动画不会执行

CGPathAddArc(path, NULL, view.centerX, view.centerY, 1, 0,M_PI * 2, 1);

CAKeyframeAnimation * animation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];

    animation.path = path;

    CGPathRelease(path);

    animation.duration = 3;

    animation.repeatCount = 500;

    animation.autoreverses = NO;

    animation.rotationMode =kCAAnimationRotateAuto;

    animation.fillMode =kCAFillModeForwards;

    [layer addAnimation:animation forKey:nil];

 [view.layer addAnimation:animation2 forKey:nil];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: