UI基础 - UIBezierPath:反转路径 | 虚线
反转路径 | 虚线
1 - 反转路径 bezierPathByReversingPath:它并不会去修改一条路径的形状, 仅仅是改变了绘制路径的方向,就是从结束点到开始点反向绘制
1 - (void)drawRect:(CGRect)rect{ 2 3 // 第一条:红色路径 4 UIBezierPath *path = [UIBezierPath bezierPath]; 5 path.lineWidth = 8; 6 [path moveToPoint: CGPointMake(10, 120)]; 7 [path addLineToPoint: CGPointMake(180, 40)]; 8 [path addLineToPoint: CGPointMake(40, 80)]; 9 [path addLineToPoint: CGPointMake(140, 140)]; 10 // 屏幕中心坐标 11 [path addLineToPoint: CGPointMake(self.frame.size.width*0.5,self.frame.size.height*0.5)]; 12 // 为了区分不同路径,这里把红色路径进行平移 13 CGAffineTransform transform = CGAffineTransformMakeTranslation(160, 0); 14 [path applyTransform:transform]; 15 [[UIColor redColor] set]; 16 [path stroke]; 17 18 // 反转路径 19 UIBezierPath *reversingPath = [path bezierPathByReversingPath]; 20 reversingPath.lineWidth = 2; 21 [reversingPath addLineToPoint: CGPointMake(10, 10)]; 22 [reversingPath addLineToPoint: CGPointMake(self.frame.size.width*0.5, self.frame.size.height*0.5)]; 23 [[UIColor blackColor] set]; 24 [reversingPath stroke]; 25 }
运行效果
2 - 虚线:setLineDash
1 -(void)drawRect:(CGRect)rect{ 2 3 // 路径 0 4 UIBezierPath *path0 = [UIBezierPath bezierPath]; 5 [path0 moveToPoint: CGPointMake(80, 40)]; 6 [path0 addLineToPoint: CGPointMake(self.frame.size.width - 40, 40)]; 7 path0.lineWidth = 2; 8 9 // 路径 1 10 UIBezierPath *path1 = [UIBezierPath bezierPath]; 11 [path1 moveToPoint: CGPointMake(80, 80)]; 12 [path1 addLineToPoint: CGPointMake(self.frame.size.width - 40, 80)]; 13 path1.lineWidth = 2; 14 15 // 路径 2 16 UIBezierPath *path2 = [UIBezierPath bezierPath]; 17 [path2 moveToPoint: CGPointMake(80, 120)]; 18 [path2 addLineToPoint: CGPointMake(self.frame.size.width - 40, 120)]; 19 path2.lineWidth = 2; 20 21 // 分别配置 3 条路径虚线的规格 22 CGFloat dashLineConfig[] = {8.0, 4.0}; 23 [path0 setLineDash: dashLineConfig 24 count: 2 25 phase: 0]; 26 27 28 CGFloat dashLineConfig1[] = {8.0, 4.0, 16.0, 8.0}; 29 [path1 setLineDash: dashLineConfig1 30 count: 4 31 phase: 0]; 32 33 34 CGFloat dashLineConfig2[] = {8.0, 4.0, 16.0, 8.0}; 35 [path2 setLineDash: dashLineConfig2 36 count: 4 37 phase: 12]; 38 39 // 绘制 40 [[UIColor orangeColor] set]; 41 [path0 stroke]; 42 [path1 stroke]; 43 [path2 stroke]; 44 45 }
运行效果:橘黄部分是实际效果图