//聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄
/**
注意點(diǎn): 1.看 GIF 效果圖.
2.看連線視圖的效果圖.
3.看實(shí)現(xiàn)代碼(直接復(fù)制實(shí)現(xiàn)效果).
*/
一、GIF 效果圖:
二、連線視圖的效果圖:
圖1:
圖2:
三、實(shí)現(xiàn)代碼:
=========================
===================================================
==========================
控制器1:SHContext.h
//
//? SHContext.h
//? PathDrawing(路徑圖)~demo
//
//? Created by石虎on 2017/8/15.
//? Copyright ? 2017年shihu. All rights reserved.
//
#ifndef SHContext_h
#define SHContext_h
/*
該方法負(fù)責(zé)繪制圓角矩形
x1、y2:是圓角矩形左上角的座標(biāo)。
width、height:控制圓角舉行的寬、高
radius:控制圓角矩形的四個(gè)圓角的半徑
*/
voidCGContextAddRoundRect(CGContextRefc,CGFloatx1 ,CGFloaty1
,CGFloatwidth ,CGFloatheight ,CGFloatradius)
{
//移動(dòng)到左上角
CGContextMoveToPoint(c, x1 + radius , y1);
//添加一條連接到右上角的線段
CGContextAddLineToPoint(c , x1 + width - radius, y1);
//添加一段圓弧
CGContextAddArcToPoint(c , x1 + width , y1, x1 + width
, y1 + radius, radius);
//添加一條連接到右下角的線段
CGContextAddLineToPoint(c , x1 + width, y1 + height - radius);
//添加一段圓弧
CGContextAddArcToPoint(c , x1 + width, y1 + height
, x1 + width - radius , y1 + height , radius);
//添加一條連接到左下角的線段
CGContextAddLineToPoint(c , x1 + radius, y1 + height);
//添加一段圓弧
CGContextAddArcToPoint(c , x1, y1 + height , x1
, y1 + height - radius , radius);
//添加一條連接到左上角的線段
CGContextAddLineToPoint(c , x1 , y1 + radius);
//添加一段圓弧
CGContextAddArcToPoint(c , x1 , y1 , x1 + radius , y1 , radius);
}
/*
該方法負(fù)責(zé)繪制多角星。
n:該參數(shù)通常應(yīng)設(shè)為奇數(shù),控制繪制N角星。
dx、dy:控制N角星的中心。
size:控制N角星的大小
*/
voidCGContextAddStar(CGContextRefc ,NSIntegern
,CGFloatdx ,CGFloatdy ,NSIntegersize)
{
CGFloatdig =4*M_PI/ n ;
//移動(dòng)到指定點(diǎn)
CGContextMoveToPoint(c , dx , dy + size);
for(inti =1; i <= n ; i++)
{
CGFloatx =sin(i * dig);
CGFloaty =cos(i * dig);
//繪制從當(dāng)前點(diǎn)連接到指定點(diǎn)的線條
CGContextAddLineToPoint(c , x * size + dx ,y * size + dy);
}
}
#endif/* SHContext_h */
=========================
===================================================
控制器2:SHPathDrawingView.m
//
//? SHPathDrawingView.m
//? PathDrawing(路徑圖)~demo
//
//? Created by石虎on 2017/8/15.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import"SHPathDrawingView.h"
#import"SHContext.h"
@implementationSHPathDrawingView
- (void)drawRect:(CGRect)rect
{
//獲取繪圖CGContextRef
CGContextRefctx =UIGraphicsGetCurrentContext();
//開(kāi)始添加路徑
CGContextBeginPath(ctx);
//添加一個(gè)五角星的路徑
CGContextAddStar(ctx,5,80,150,40);
//添加一個(gè)圓角矩形的路徑
CGContextAddRoundRect(ctx,10,30,150,70,14);
//關(guān)閉路徑
CGContextClosePath(ctx);
//設(shè)置線條顏色
CGContextSetRGBStrokeColor(ctx,1,1,0,1);
//設(shè)置線寬
CGContextSetLineWidth(ctx,4);
//繪制路徑
CGContextStrokePath(ctx);
//開(kāi)始添加路徑
CGContextBeginPath(ctx);
//添加一個(gè)五角星的路徑
CGContextAddStar(ctx,5,240,150,40);
//添加一個(gè)圓角矩形的路徑
CGContextAddRoundRect(ctx,170,30,130,70,14);
//關(guān)閉路徑
CGContextClosePath(ctx);
//設(shè)置填充顏色
CGContextSetRGBFillColor(ctx,1,0,1,1);
//采用填充并繪制路徑的方式來(lái)繪制路徑
CGContextDrawPath(ctx, kCGPathFillStroke);
//開(kāi)始添加路徑
CGContextBeginPath(ctx);
//添加一個(gè)3角星的路徑
CGContextAddStar(ctx,3,60,220,40);
//關(guān)閉路徑
CGContextClosePath(ctx);
//設(shè)置填充顏色
CGContextSetRGBFillColor(ctx,1,0,0,1);
//填充路徑
CGContextFillPath(ctx);
//開(kāi)始添加路徑
CGContextBeginPath(ctx);
//添加一個(gè)7角星的路徑
CGContextAddStar(ctx,7,160,220,40);
//關(guān)閉路徑
CGContextClosePath(ctx);
//設(shè)置填充顏色
CGContextSetRGBFillColor(ctx,0,1,0,1);
//填充路徑
CGContextFillPath(ctx);
//開(kāi)始添加路徑
CGContextBeginPath(ctx);
//添加一個(gè)9角星的路徑
CGContextAddStar(ctx,9,260,220,40);
//關(guān)閉路徑
CGContextClosePath(ctx);
//設(shè)置填充顏色
CGContextSetRGBFillColor(ctx,0,0,1,1);
//填充路徑
CGContextFillPath(ctx);
}
@end
=========================
===================================================
謝謝!!!