UIBezierPath简介
UIBezierPath类是Core Graphics框架关于path的一个封装。可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线段组成的形状。
为什么要用UIBezierPath绘图。。?
上文主要介绍了C语言的绘图方式,如果使用上文所说的C语言方式绘图代码就比较多,使用贝塞尔路径来实现绘图,使用起来比较方便,易阅读 ,善于管理,不像上面那种阅读起来比较麻烦,相对于C语言的Core Graphics来说更为平易近人。它能够使用ARC,所以今后绘图建议使用BezierPath。
矩形圆角设置(常用用于UIIView,UIImageView圆角设置)
1 2 3 4 5 6 7
| typedef NS_OPTIONS(NSUInteger, UIRectCorner) { UIRectCornerTopLeft = 1 << 0, UIRectCornerTopRight = 1 << 1, UIRectCornerBottomLeft = 1 << 2, UIRectCornerBottomRight = 1 << 3, UIRectCornerAllCorners = ~0UL };
|
UIBezierPath对象对象创建方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| + (instancetype)bezierPath;
+ (instancetype)bezierPathWithRect:(CGRect)rect;
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;
|
相关属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| @property(nonatomic) CGPathRef CGPath; - (CGPathRef)CGPath NS_RETURNS_INNER_POINTER CF_RETURNS_NOT_RETAINED;
@property(readonly,getter=isEmpty) BOOL empty;
@property(nonatomic,readonly) CGRect bounds;图形路径中的当前点
@property(nonatomic,readonly) CGPoint currentPoint;
@property(nonatomic) CGFloat lineWidth;
@property(nonatomic) CGLineCap lineCapStyle; typedef CF_ENUM(int32_t, CGLineCap) { kCGLineCapButt, kCGLineCapRound, kCGLineCapSquare };
@property(nonatomic) CGLineJoin lineJoinStyle; typedef CF_ENUM(int32_t, CGLineJoin) { kCGLineJoinMiter, kCGLineJoinRound, kCGLineJoinBevel };
@property(nonatomic) CGFloat miterLimit;
@property(nonatomic) CGFloat flatness;
@property(nonatomic) BOOL usesEvenOddFillRule;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| - (void)moveToPoint:(CGPoint)point;
- (void)addLineToPoint:(CGPoint)point;
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise NS_AVAILABLE_IOS(4_0);
- (void)closePath;
- (void)removeAllPoints;
- (void)appendPath:(UIBezierPath *)bezierPath;
- (UIBezierPath *)bezierPathByReversingPath NS_AVAILABLE_IOS(6_0);
- (void)applyTransform:(CGAffineTransform)transform;
|
1 2 3
| - (void)fill; - (void)stroke; - (void)addClip;
|
画图示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| - (void)drawRect2:(CGRect)rect { [[UIColor redColor] setStroke];
UIBezierPath *bezierPath = [UIBezierPath bezierPath]; bezierPath.lineWidth = 10; bezierPath.lineCapStyle = kCGLineCapRound; [bezierPath moveToPoint:CGPointMake(10, 10)]; [bezierPath addLineToPoint:CGPointMake(100, 100)]; [bezierPath stroke]; }
- (void)drawRect3:(CGRect)rect { [[UIColor redColor] setStroke];
UIBezierPath *bezierPath = [UIBezierPath bezierPath]; bezierPath.lineWidth = 10; bezierPath.lineCapStyle = kCGLineCapRound; [bezierPath moveToPoint:CGPointMake(100, 100)]; [bezierPath addQuadCurveToPoint:CGPointMake(200, 200) controlPoint:CGPointMake(300, 0)];
[bezierPath stroke]; }
- (void)drawRect4:(CGRect)rect { [[UIColor redColor] setStroke];
UIBezierPath *bezierPath = [UIBezierPath bezierPath]; bezierPath.lineWidth = 10; bezierPath.lineCapStyle = kCGLineCapRound; [bezierPath moveToPoint:CGPointMake(100, 100)]; [bezierPath addCurveToPoint:CGPointMake(200, 200) controlPoint1:CGPointMake(150, 0) controlPoint2:CGPointMake(150, 300)];
[bezierPath stroke]; }
- (void)drawRect5:(CGRect)rect { [[UIColor redColor] setStroke];
UIBezierPath *bezierPath = [UIBezierPath bezierPath]; bezierPath.lineWidth = 10; bezierPath.lineCapStyle = kCGLineCapRound; [bezierPath moveToPoint:CGPointMake(100, 100)]; [bezierPath addCurveToPoint:CGPointMake(200, 200) controlPoint1:CGPointMake(150, 0) controlPoint2:CGPointMake(150, 300)];
UIBezierPath *bezierPath2 = [UIBezierPath bezierPath]; [bezierPath2 moveToPoint:CGPointMake(200, 200)]; [bezierPath2 addCurveToPoint:CGPointMake(290, 290) controlPoint1:CGPointMake(250, 0) controlPoint2:CGPointMake(250, 300)]; [bezierPath appendPath:bezierPath2];
[bezierPath stroke]; }
#pragma mark---
- (void)drawRect10:(CGRect)rect { [[UIColor redColor] setStroke];
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 280, 280)]; bezierPath.lineWidth = 10; bezierPath.lineCapStyle = kCGLineCapRound;
[bezierPath stroke]; }
- (void)drawRect11:(CGRect)rect { [[UIColor redColor] setStroke];
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 280, 280) cornerRadius:30]; bezierPath.lineWidth = 10;
[bezierPath stroke]; }
- (void)drawRect12:(CGRect)rect { [[UIColor redColor] setStroke];
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 280, 280) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)]; bezierPath.lineWidth = 10;
[bezierPath stroke]; }
- (void)drawRect13:(CGRect)rect { [[UIColor redColor] setStroke];
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 280, 280)]; bezierPath.lineWidth = 10; bezierPath.lineCapStyle = kCGLineCapRound;
[bezierPath stroke]; }
- (void)drawRect14:(CGRect)rect { [[UIColor redColor] setStroke];
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:110 startAngle:0 endAngle:M_PI_2 clockwise:NO]; bezierPath.lineWidth = 10;
[bezierPath stroke]; }
|
工具推荐
https://github.com/YouXianMing/Tween-o-Matic-CN这是个自定义贝塞尔曲线值的mac工具
由于图像绘制代码一般较多,下面这些工具可以 简单快速生成绘制代码,实时查看效果,简单高效,生成的代码也很规范。
1、paintCode
PaintCode是由来自斯洛伐克首都伯拉第斯拉瓦的PixelCut软件公司推出的,一款面向iOS和Mac应用开发者及设计师的矢量图形可视化开发工具。通过PaintCode,即使是没有编程经验的设计师也能绘制出精美的控件、图标及其他UI元素。而PaintCode最为显著的一点就是能够直接生成适用于iOS的objectivec或Swift的代码,节省了大量的编程时间,也正因如此,许多开发者将其称为设计与开发通吃的代码神器。
2、QuartzCode
官网:http://www.quartzcodeapp.com/
QuartzCode 是一个快速的、 轻量级的、 强大的动画工具,转换矢量绘图和动画到Object C 和 Swift 代码。我们只需更改属性 ,还可以可以循环在几秒钟内,实时看到动画的变化。减少了在 Xcode 创建动画的障碍 !
3、Sketch 矢量图设计工具
http://www.sketchcn.com/