一般只能在drawRet中繪制。但是結(jié)合 CAShapeLayer折砸,繪制到layer上在add到View上就能隨機(jī)繪制了。這里只講UIBezierPath。CAShapeLayer 很簡(jiǎn)單丈挟,看另外一篇好了。
指定圖形
UIBezierPath *path1 = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 400, 100)];// 矩形
UIBezierPath *path2 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(90, 90, 120, 120)];// 內(nèi)接圓
UIBezierPath *path3 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(90, 90, 120, 120) cornerRadius:12];// 圓角矩形
UIBezierPath *path4 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(90, 90, 120, 120) byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(20, 2)];// 選擇性圓角矩形
/*
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};// 哪個(gè)角
*/
UIBezierPath *path5 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(120, 120) radius:120 startAngle:0 endAngle:M_PI/2 clockwise:NO];// 弧線(xiàn)志电。(0 - 正x軸曙咽,2M_PI 是一圈。clockwise :是順逆時(shí)針)
// 注意 挑辆,只有圖形是不夠滴例朱,還需要對(duì)圖形添加屬性,以及繪制之拨,下面講茉继。
自定義 圖形
// 初始化
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100, 100)];// 起點(diǎn)
[path addLineToPoint:CGPointMake(120,120)];// 直線(xiàn)終點(diǎn)
[path addCurveToPoint:CGPointMake(100, 200) controlPoint1:CGPointMake(120, 220) controlPoint2:CGPointMake(100, 300)];// 2次曲線(xiàn)
[path addQuadCurveToPoint:CGPointMake(100, 200) controlPoint:CGPointMake(140, 230)];// 1次曲線(xiàn)
[path addArcWithCenter:CGPointMake(200, 200) radius:80 startAngle:0 endAngle:M_PI clockwise:YES];// 圓弧
[path closePath];// 首位相連
[path appendPath:path2];// 拼接路徑
path = [path bezierPathByReversingPath];// 路徑方向相反,圖形是一樣的
[path removeAllPoints];// 刪除所有路徑
// 3D變化蚀乔,需要好好研究0.0
[path applyTransform:CGAffineTransform];
路徑屬性 與 繪制
- 基本繪制
path.lineWidth = 1.;// 線(xiàn)寬
// 顏色 填充色烁竭,描邊色
[[UIColor redColor] setFill];
[[UIColor greenColor] setStroke];
// 繪制
[path fill];
[path stroke];
// 裁剪,作用不明
[path addClip];
- 更多好玩的屬性
path.miterLimit = 2.;// 防止轉(zhuǎn)折處尖叫過(guò)長(zhǎng)
path.flatness = 2.;// 根據(jù)彎曲程度吉挣,決定渲染精度派撕?
path.usesEvenOddFillRule = NO;// 這個(gè)牛逼了,圖形復(fù)雜是填充顏色的一種規(guī)則睬魂。類(lèi)似棋盤(pán)终吼。
path.lineCapStyle = kCGLineCapRound;// 兩端
/*
typedef CF_ENUM(int32_t, CGLineCap) {
kCGLineCapButt,
kCGLineCapRound,
kCGLineCapSquare
};
*/
path.lineJoinStyle = kCGLineJoinRound;// 轉(zhuǎn)折
/*
typedef CF_ENUM(int32_t, CGLineJoin) {
kCGLineJoinMiter,
kCGLineJoinRound,
kCGLineJoinBevel
};
*/
獲取 屬性
@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;
- (BOOL)containsPoint:(CGPoint)point;
其他
- addClip:作用不明
- (void)applyTransform:(CGAffineTransform)transform:水太深。
1