如何用UIBezierPath画线
来源:互联网 发布:在淘宝买药可靠吗 编辑:程序博客网 时间:2024/04/27 22:34
本文讲述如何在UIView的子类中使用UIBezierPath来画图, 一般的使用方法都是在- (void)drawRect:(CGRect)rect的上下文中来画图. 现在我们来讲一讲如何不用指定上下文,在UIView中使用UIBezierPath.
一. 在子类中重写类方法+layerClass, 指定创建的图层为类CAShapeLayer
// OTDrawView.m+ (Class)layerClass { return[CAShapeLayer class];}
二. 初始化
创建一个数组存储每次按下时点的位置, 设置背景为白色, 并声明我们不使用填充及指定画线的颜色.
- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { //Initialization code points = [[NSMutableArray alloc] init]; self.backgroundColor = [UIColor whiteColor]; ((CAShapeLayer *)self.layer).fillColor = nil; ((CAShapeLayer *)self.layer).strokeColor = [UIColor blackColor].CGColor; } return self;}
三. 更新路径函数, 使用UIBezierPath根据记录的points数组, 按顺序将每条线描绘出来. 最重要的地方是把UIBezierPath的值赋给((CAShapeLayer *)self.layer).path
- (void)updatePaths{ if ([points count] >= 2) { UIBezierPath *path = [[UIBezierPath alloc] init]; [path moveToPoint:[[points firstObject] CGPointValue]]; NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, [points count] - 1)]; [points enumerateObjectsAtIndexes:indexSet options:0 usingBlock:^(NSValue *pointValue, NSUInteger idx, BOOL *stop) { [path addLineToPoint:[pointValue CGPointValue]]; }]; ((CAShapeLayer *)self.layer).path = path.CGPath; } else { ((CAShapeLayer *)self.layer).path = nil; }}
四. 这是我们每个点生成的地方, 每按下一次屏幕, 就会把当前点存入points数组中, 再调用updatePaths更新路径.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch =[touches anyObject]; CGPoint point = [touch locationInView:self]; [points addObject:[NSValue valueWithCGPoint:point]]; [self updatePaths];}
1 0
- 如何用UIBezierPath画线
- [脚本] Unity3d 如何用GL画线
- iOS 使用UIBezierPath画线
- UIBezierPath 画线 圆 弧
- 如何用C做出表格效果(非简单画线)
- 使用UIBezierPath平滑的画线
- 使用UIBezierPath平滑的画线
- UIView上使用UIBezierPath画线
- iOS CAShapeLayer & UIBezierPath画线、画图
- 【iOS开发-画线】使用贝塞尔曲线(UIBezierPath)画线
- [ios]ios画线 使用CGContextRef,CGPath和UIBezierPath来绘画
- 如何用2.0发信?
- 想知道socket如何用
- 如何用英语杀价?
- 如何用C画图
- 问如何用文件
- 如何用JSP编程
- 如何用PS切图
- js上传图片预览加实时剪切
- SQL Server安装程序无法获取ASPNET账户的系统账户信息
- JS正则表达式大全
- html 1.1
- 存储数据键和项目对的类(Dictionary对象)
- 如何用UIBezierPath画线
- 今天的收获
- 我读过的最好的epoll讲解--转自”知乎“
- deb包转化成rpm包
- MySQL 4.1/5.0/5.1/5.5/5.6各版本的主要区别
- 无限级目录树最优算法的新研究
- 第7周作业1——背包问题
- Uva-10131 Is Bigger Smarter?
- 使用xmlhttp为网站增加股市行情查询功能