本文只为记录,自学 CAAnaimation 中正在学习中...
2016-02-16 15:50
525 查看
#import "ViewController.h"
@interface
ViewController ()
{
/**
* angle :旋转的弧度
需要转换成角度
* rotation_X:向Y轴方向旋转。
* rotation_Y:向Y轴方向旋转。
* rotation_Z:向Z轴方向旋转。
*/
float angle;
float rotation_X;
float rotation_Y;
float rotation_Z;
BOOL isBoundary;
CALayer *layer;
CATransform3D Trans3D;
dispatch_source_t _timer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// IOS--CALayer实现,界限、透明度、位置、旋转、缩放组合动画(转)
// http://www.cocoachina.com/bbs/read.php?tid=124506
// iOS开发基础知识:Core Animation(核心动画)
// http://www.jianshu.com/p/8c1c1697c0ce 本工程参考网址
// 非常清楚的CATransform3D
讲解
// http://blog.sina.com.cn/s/blog_51a995b70101mz3q.html 参考网址
// 一.
// CATransform3D CATransform3DMakeTranslation (CGFloat tx, CGFloat ty, CGFloat tz)
// tx:X轴偏移位置,往下为正数。
// ty:Y轴偏移位置,往右为正数。
// tz:Z轴偏移位置,往外为正数。
// 二.
// CATransform3D CATransform3DTranslate (CATransform3D t, CGFloat tx, CGFloat ty, CGFloat tz);
// t:就是上一个函数。其他的都一样。
// 就可以理解为:函数的叠加,效果的叠加。
// 三.
// CATransform3D CATransform3DMakeScale (CGFloat sx, CGFloat sy, CGFloat sz);
// sx:X轴缩放,代表一个缩放比例,一般都是 0 --- 1
之间的数字。
// sy:Y轴缩放。
// sz:整体比例变换时,也就是m11(sx)==
m22(sy)时,若m33(sz)>1,图形整体缩小,若0<1,图形整体放大,若m33(sz)<0,发生关于原点的对称等比变换。
// 四.
// CATransform3D CATransform3DMakeRotation(190*M_PI/180.0, 0, 0, 0);
// 旋转效果。
// angle:旋转的弧度,所以要把角度转换成弧度:角度 * M_PI / 180。
// x:向X轴方向旋转。值范围-1 --- 1之间
// y:向Y轴方向旋转。值范围-1 --- 1之间
// z:向Z轴方向旋转。值范围-1 --- 1之间
isBoundary =
YES;
[self
BasicAnimation2];
NSTimeInterval period =
.10; //设置时间间隔
dispatch_queue_t queue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0);
_timer =
dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0, 0, queue);
dispatch_source_set_timer(_timer,
dispatch_walltime(NULL,
0), period *
NSEC_PER_SEC,
0);
//每秒执行
dispatch_source_set_event_handler(_timer, ^{
//在这里执行事件
if (-1.0<=rotation_X<=1.0||0.0<=angle<=360.0)
{
if (isBoundary) {
rotation_X+=.1;
rotation_Y+=.1;
rotation_Z+=.1;
angle+=36;
}
}else{
isBoundary =
NO;
rotation_X-=.1;
rotation_Y-=.1;
rotation_Z-=.1;
angle-=36;
if (rotation_X==-1||angle==0) {
isBoundary=
YES;
}
}
dispatch_async(dispatch_get_main_queue(), ^{
Trans3D =
CATransform3DMakeRotation(angle*M_PI/180.0,
rotation_X, rotation_Y,
rotation_Z);
layer.transform =
Trans3D;
});
});
dispatch_resume(_timer);
}
- (void)BasicAnimation2{
// CATransform3D Trans3D = CATransform3DMakeScale(1, 1, 1);
//创建一个CALayer
layer = [CALayer
layer];
//设置背景颜色
类型为CGColorRef
layer.backgroundColor = [UIColor
blueColor].CGColor;
//设置中心点
layer.position =
self.view.center;
layer.transform =
Trans3D;
//设置大小
layer.bounds =
CGRectMake(0,
0, 100,
100);
//添加到self.view的layer上
[self.view.layer
addSublayer:layer];
//创建一个基本动画
CABasicAnimation *basic = [CABasicAnimation
animation];
//动画时间
basic.duration =
2.0;
//重复次数
basic.repeatCount =
MAXFLOAT;
//通过指定CALayer的一个属性名称为keyPath(NSString类型),并且对CALayer的这个属性的值进行修改,达到相应的动画效果。比如,指定@“position”为keyPath,就修改CALayer的position属性的值,以达到平移的动画效果
//keyPath内容是CALayer的可动画Animatable属性
basic.keyPath =
@"transform.scale";
//keyPath相应属性的初始值
basic.fromValue =
@1;
//keyPath相应属性的结束值
basic.toValue =
@1;
//layer
添加一个基础动画
// [layer addAnimation:basic forKey:nil];
//创建一个CABasicAnimation对象
(改变颜色动画)
CABasicAnimation *animation=[CABasicAnimation
animation];
//设置颜色
animation.toValue=(id)[UIColor
redColor].CGColor;
//动画时间
animation.duration=1;
//重复次数
animation.repeatCount =
MAXFLOAT;
//是否反转变为原来的属性值
animation.autoreverses=NO;
//把animation添加到图层的layer中,便可以播放动画了。forKey指定要应用此动画的属性
[layer addAnimation:animation
forKey:@"backgroundColor"];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
@interface
ViewController ()
{
/**
* angle :旋转的弧度
需要转换成角度
* rotation_X:向Y轴方向旋转。
* rotation_Y:向Y轴方向旋转。
* rotation_Z:向Z轴方向旋转。
*/
float angle;
float rotation_X;
float rotation_Y;
float rotation_Z;
BOOL isBoundary;
CALayer *layer;
CATransform3D Trans3D;
dispatch_source_t _timer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// IOS--CALayer实现,界限、透明度、位置、旋转、缩放组合动画(转)
// http://www.cocoachina.com/bbs/read.php?tid=124506
// iOS开发基础知识:Core Animation(核心动画)
// http://www.jianshu.com/p/8c1c1697c0ce 本工程参考网址
// 非常清楚的CATransform3D
讲解
// http://blog.sina.com.cn/s/blog_51a995b70101mz3q.html 参考网址
// 一.
// CATransform3D CATransform3DMakeTranslation (CGFloat tx, CGFloat ty, CGFloat tz)
// tx:X轴偏移位置,往下为正数。
// ty:Y轴偏移位置,往右为正数。
// tz:Z轴偏移位置,往外为正数。
// 二.
// CATransform3D CATransform3DTranslate (CATransform3D t, CGFloat tx, CGFloat ty, CGFloat tz);
// t:就是上一个函数。其他的都一样。
// 就可以理解为:函数的叠加,效果的叠加。
// 三.
// CATransform3D CATransform3DMakeScale (CGFloat sx, CGFloat sy, CGFloat sz);
// sx:X轴缩放,代表一个缩放比例,一般都是 0 --- 1
之间的数字。
// sy:Y轴缩放。
// sz:整体比例变换时,也就是m11(sx)==
m22(sy)时,若m33(sz)>1,图形整体缩小,若0<1,图形整体放大,若m33(sz)<0,发生关于原点的对称等比变换。
// 四.
// CATransform3D CATransform3DMakeRotation(190*M_PI/180.0, 0, 0, 0);
// 旋转效果。
// angle:旋转的弧度,所以要把角度转换成弧度:角度 * M_PI / 180。
// x:向X轴方向旋转。值范围-1 --- 1之间
// y:向Y轴方向旋转。值范围-1 --- 1之间
// z:向Z轴方向旋转。值范围-1 --- 1之间
isBoundary =
YES;
[self
BasicAnimation2];
NSTimeInterval period =
.10; //设置时间间隔
dispatch_queue_t queue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0);
_timer =
dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0, 0, queue);
dispatch_source_set_timer(_timer,
dispatch_walltime(NULL,
0), period *
NSEC_PER_SEC,
0);
//每秒执行
dispatch_source_set_event_handler(_timer, ^{
//在这里执行事件
if (-1.0<=rotation_X<=1.0||0.0<=angle<=360.0)
{
if (isBoundary) {
rotation_X+=.1;
rotation_Y+=.1;
rotation_Z+=.1;
angle+=36;
}
}else{
isBoundary =
NO;
rotation_X-=.1;
rotation_Y-=.1;
rotation_Z-=.1;
angle-=36;
if (rotation_X==-1||angle==0) {
isBoundary=
YES;
}
}
dispatch_async(dispatch_get_main_queue(), ^{
Trans3D =
CATransform3DMakeRotation(angle*M_PI/180.0,
rotation_X, rotation_Y,
rotation_Z);
layer.transform =
Trans3D;
});
});
dispatch_resume(_timer);
}
- (void)BasicAnimation2{
// CATransform3D Trans3D = CATransform3DMakeScale(1, 1, 1);
//创建一个CALayer
layer = [CALayer
layer];
//设置背景颜色
类型为CGColorRef
layer.backgroundColor = [UIColor
blueColor].CGColor;
//设置中心点
layer.position =
self.view.center;
layer.transform =
Trans3D;
//设置大小
layer.bounds =
CGRectMake(0,
0, 100,
100);
//添加到self.view的layer上
[self.view.layer
addSublayer:layer];
//创建一个基本动画
CABasicAnimation *basic = [CABasicAnimation
animation];
//动画时间
basic.duration =
2.0;
//重复次数
basic.repeatCount =
MAXFLOAT;
//通过指定CALayer的一个属性名称为keyPath(NSString类型),并且对CALayer的这个属性的值进行修改,达到相应的动画效果。比如,指定@“position”为keyPath,就修改CALayer的position属性的值,以达到平移的动画效果
//keyPath内容是CALayer的可动画Animatable属性
basic.keyPath =
@"transform.scale";
//keyPath相应属性的初始值
basic.fromValue =
@1;
//keyPath相应属性的结束值
basic.toValue =
@1;
//layer
添加一个基础动画
// [layer addAnimation:basic forKey:nil];
//创建一个CABasicAnimation对象
(改变颜色动画)
CABasicAnimation *animation=[CABasicAnimation
animation];
//设置颜色
animation.toValue=(id)[UIColor
redColor].CGColor;
//动画时间
animation.duration=1;
//重复次数
animation.repeatCount =
MAXFLOAT;
//是否反转变为原来的属性值
animation.autoreverses=NO;
//把animation添加到图层的layer中,便可以播放动画了。forKey指定要应用此动画的属性
[layer addAnimation:animation
forKey:@"backgroundColor"];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- Keychains中此证书签发者无效的解决方法
- 一次Hadoop脑裂的修复(Cloudera hadoop split-brain repair)
- 浅谈xaingce apk样本分析
- Rails Commands
- python的类变量和实例变量 http://blog.csdn.net/chenggong2dm/article/details/9030481
- Rails Commands
- TairResult 缓存 计数器的使用
- 正常使用谷歌搜索引擎和Gmail的方法!
- 在mac上配置git send-email使用gmail
- Contains Duplicate,Contains Duplicate II,Contains Duplicate III
- 【DP】AIM Tech Round (Div. 2) D
- QMainWindow布局和QDialog布局的区别
- 500 OOPS: vsftpd: refusing to run with writable anonymous root Login failed.
- cvpr读书笔记[3]:traincascade与AdaBoost的opencv实现框架
- may be using a version of Gradle that does not contain the method.
- Failed to locate or generate matching signing assets Xcode attempted to locate or generate matching
- HDU 2675 Equation Again
- 【机器学习实战】Naive Bayes
- Snaigt 12.4.0 的使用和Snagit KEY
- 使用mdadm创建软raid