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];
上代码
第一种:使用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];
相关文章推荐
- 密码中有特殊的字符IOS如何处理 IOS中URL包含中文参数的问题
- iOS系统版本比较的宏使用
- IOS事件处理机制(关于触发者和响应者的确认)
- iOS CAGradientLayer 详解
- iOS端项目注释规范附统一代码块
- 在iOS中使用手指简单画线
- IOS视频压缩
- iOS安全:反汇编(inline编译方式)
- 适配低版本和高版本系统的API(转载)
- iOS 活动图 流程图
- iOS推送证书生成
- 如何设计一个 iOS 控件?(iOS 控件完全解析)
- iOS常用封装方法
- How to Disable System Integrity Protection
- IOS博客项目搭建-06-自定义TabBarButton-03
- iOS 【CocoaPods 全攻略】
- iOS bitcode实战
- 【iOS开发】关于awakeFromNib的使用
- IOS9通用链接使用要点
- iOS文字排版(CoreText)那些事儿