CoreAnimation-06-CAKeyframeAnimation 相关代码
2015-08-31 09:57
351 查看
- (void)awakeFromNib{
iMV = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
iMV.image = [UIImage imageNamed:@"c"];
[self addSubview:iMV];
NSLog(@"%@",NSStringFromCGRect([[self.subviews firstObject] layer].bounds));
}
使用成员属性保存贝瑟尔路径
监听触摸事件的状态,绘制贝瑟尔曲线
开始
移动
结束(创建动画)
将路径显示到图层上
iMV = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
iMV.image = [UIImage imageNamed:@"c"];
[self addSubview:iMV];
NSLog(@"%@",NSStringFromCGRect([[self.subviews firstObject] layer].bounds));
}
使用成员属性保存贝瑟尔路径
@property (nonatomic, strong) UIBezierPath *path;
监听触摸事件的状态,绘制贝瑟尔曲线
开始
//确定起点 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //获取当前触摸点 UITouch *touch = [touches anyObject]; CGPoint curretnPoint = [touch locationInView:self]; //创建路径 UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:curretnPoint]; //保存路径 self.path = path; }
移动
//添加线条 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { //获取当前触摸点 UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self]; //添加线条 [self.path addLineToPoint:currentPoint]; //重绘,将曲线显示到图层上 [self setNeedsDisplay]; }
结束(创建动画)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { //创建动画 CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]; //指定执行动画的属性, animation.keyPath = @"position"; //设置动画的执行路径 animation.path = self.path.CGPath; //设置动画的执行时间 animation.duration = 1; //设置动画的重复次数 animation.repeatCount = MAXFLOAT; //将动画添加到对应的图层上 [[[self.subviews firstObject] layer] addAnimation:animation forKey:nil]; }
将路径显示到图层上
//绘制路径 - (void)drawRect:(CGRect)rect { [self.path stroke]; }
相关文章推荐
- 使用eclipse调试SylixOS应用
- 【Java】Java单例模式的七种写法 (全)
- 【java】贪吃蛇小游戏
- C++11模版元编程的应用
- C++四种强制类型转换运算符
- Python, pitfall of creating 2d list, [foo*3]*3
- eclipse中如何导入jar包源码
- c#导入导出excel(无格式控制)
- c#基础笔记(1) 枚举
- Json for Java
- 使用JavaIO提供的API下载指定文件(image)
- 报表导出到word或者excel的功能实现
- 浅析Java内存模型( 深入理解Java虚拟机))
- 【struts2】struts2配置中Action的name和package的name和namespace有什么作用 ?
- windows和ubuntu下使用python写备份脚本
- Java多线程编程技术
- 【编程练习】3*3 的矩阵,值限定为1-9不重复,已知横竖的和,和一个斜着的值,求这个矩阵
- C# WinForm获取当前路径汇总
- Windows下安装python2.7及科学计算套装
- 链表排序