CAKeyframeAnimation实现控件按照手势绘制的路径做动画
2016-04-08 20:51
369 查看
1.要绘制路径,需要自定义View
DrawView
// // DrawView.m #import "DrawView.h" @interface DrawView() @property(nonatomic,strong)UIBezierPath *path; @end @implementation DrawView - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentP = [touch locationInView:self]; // 创建路径 UIBezierPath *path = [UIBezierPath bezierPath]; _path = path; // 设置路径起点 [path moveToPoint:currentP]; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentP = [touch locationInView:self]; // 添加点到当前路径 [_path addLineToPoint:currentP]; // 重绘 [self setNeedsDisplay]; } /** * 手指立刻,让控件按照我们绘制的路径做动画 */ - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // // 创建动画 CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; anim.keyPath = @"position"; anim.path = _path.CGPath; anim.repeatCount = MAXFLOAT; anim.duration = 1; [[[self.subviews firstObject] layer] addAnimation:anim forKey:nil]; } - (void)drawRect:(CGRect)rect { [_path stroke]; } @end注意,控制器里不要实现touch等方法,不然会被拦截
相关文章推荐
- 页面执行过程
- Html网页的代码
- 在Linux下利用替换函数(exec函数家族)写一个简单的myshell
- POJ1502 MPI Maelstrom(最短路径)
- hdu 1060 Leftmost Digit
- 有向无环图(DAG)的最小路径覆盖
- TControlStyle.csParentBackground的作用(附Delphi里的所有例子,待续)
- 第四篇学习笔记
- Nagios
- Javascript编程笔记(一)
- 高盛VR/AR报告完整解读版
- FPGA零散笔记
- 封装分页查询的类
- 05章 体检套餐管理系统
- 设置时区TimeZone
- Useful Links for Deep Learning
- py-faster-rcnn配置运行faster_rcnn_end2end—VGG_CNN_M_1024(Ubuntu14.04)
- android 开发环境搭建
- An internal error occurred during: "Initializing Java Tooling". java.lang.NullPointerException
- 正确的事情要坚持做下去(一):说再见