//聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄
/**
注意點(diǎn): 1.看 GIF 效果圖.
2.看連線視圖的效果圖.
3.看實(shí)現(xiàn)代碼(直接復(fù)制實(shí)現(xiàn)效果).
*/
一拭宁、GIF 效果圖:
二嫂冻、連線視圖的效果圖:
圖1:
圖2:
圖3:
圖4:
圖5:
三、實(shí)現(xiàn)代碼:
=========================
===================================================
==========================
控制器1:SHContext.h
//
//? SHContext.h
//? HandDraw(手繪)~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:SHConstant.h
//=================
//=============
//
//? SHConstant.h
//? HandDraw(手繪)~demo
//
//? Created by石虎on 2017/8/15.
//? Copyright ? 2017年shihu. All rights reserved.
//
#ifndef SHConstant_h
#define SHConstant_h
typedefenum
{
kLineShape =0,
kRectShape,
kEllipseShape,
kRoundRectShape,
kPenShape
} ShapeType;
#endif/* SHConstant_h */
控制器3:SHDrawView.h
//=================
//=============
//
//? SHDrawView.h
//? HandDraw(手繪)~demo
//
//? Created by石虎on 2017/8/15.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import
#import"SHConstant.h"
@interfaceSHDrawView :UIView
@property(nonatomic,strong)UIColor* currentColor;
@property(nonatomic,assign)ShapeTypeshape;
@end
控制器3:SHDrawView.m
//=================
//=============
//
//? SHDrawView.m
//? HandDraw(手繪)~demo
//
//? Created by石虎on 2017/8/15.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import"SHDrawView.h"
#import"SHContext.h"
@implementationSHDrawView
CGPointfirstTouch, prevTouch, lastTouch;
//定義向內(nèi)存中圖片執(zhí)行繪圖的CGContextRef
CGContextRefbuffCtx;
UIImage* image;
- (id)initWithCoder:(NSCoder*)aCoder
{
self= [superinitWithCoder:aCoder];
if(self) {
//初始化時(shí)將當(dāng)前顏色設(shè)為紅色
self.currentColor= [UIColorredColor];
//創(chuàng)建內(nèi)存中的圖片
UIGraphicsBeginImageContext(self.bounds.size);
//獲取向內(nèi)存中圖片執(zhí)行繪圖的CGContextRef
buffCtx=UIGraphicsGetCurrentContext();
}
returnself;
}
//當(dāng)用戶手指開始觸碰時(shí)激發(fā)該方法
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch*touch = [touchesanyObject];
//獲取觸碰點(diǎn)坐標(biāo)
firstTouch= [touchlocationInView:self];
//如果當(dāng)前正在進(jìn)行自由繪制奕扣,prevTouch代表第一個(gè)觸碰點(diǎn)
if(self.shape==kPenShape)
{
prevTouch=firstTouch;
}
}
//當(dāng)用戶手指在控件上拖動(dòng)時(shí)不斷地激發(fā)該方法
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch*touch = [touchesanyObject];
//獲取觸碰點(diǎn)坐標(biāo)
lastTouch= [touchlocationInView:self];
//如果當(dāng)前正在進(jìn)行自由繪制
if(self.shape==kPenShape)
{
//向內(nèi)存中的圖片執(zhí)行繪制
[selfdraw:buffCtx];
//取出內(nèi)存中的圖片薪鹦,保存到image中
image=UIGraphicsGetImageFromCurrentImageContext();
}
//通知該控件重繪,此時(shí)會(huì)實(shí)時(shí)地繪制起始點(diǎn)與用戶手指拖動(dòng)點(diǎn)之間的形狀
[selfsetNeedsDisplay];
}
//當(dāng)用戶手指離開控件時(shí)激發(fā)該方法
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch*touch = [touchesanyObject];
//獲取離開觸碰的點(diǎn)坐標(biāo)
lastTouch= [touchlocationInView:self];
//向內(nèi)存中的圖片執(zhí)行繪制惯豆,即把最終確定圖形繪制到內(nèi)存中圖片上
[selfdraw:buffCtx];
image=UIGraphicsGetImageFromCurrentImageContext();
//通知重繪池磁。
[selfsetNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
//獲取繪圖上下文
CGContextRefctx =UIGraphicsGetCurrentContext();
//將內(nèi)存中的圖片繪制出來
[imagedrawAtPoint:CGPointZero];
//調(diào)用draw:方法執(zhí)行繪制
[selfdraw:ctx];
}
//定義一個(gè)函數(shù),用于根據(jù)firstTouch楷兽、lastTouch來確定矩形區(qū)域
- (CGRect) curRect
{
returnCGRectMake(firstTouch.x, firstTouch.y,
lastTouch.x - firstTouch.x ,
lastTouch.y - firstTouch.y);
}
- (void)draw:(CGContextRef)ctx
{
//設(shè)置線條顏色
CGContextSetStrokeColorWithColor(ctx,self.currentColor.CGColor);
//設(shè)置填充顏色
CGContextSetFillColorWithColor(ctx,self.currentColor.CGColor);
//設(shè)置線寬
CGContextSetLineWidth(ctx,2.0);
CGContextSetShouldAntialias(ctx,YES);
switch(self.shape) {
CGFloat leftTopX , leftTopY;
casekLineShape:
//添加從firstTouch到lastTouch的路徑
CGContextMoveToPoint(ctx, firstTouch.x, firstTouch.y);
CGContextAddLineToPoint(ctx, lastTouch.x, lastTouch.y);
//繪制路徑
CGContextStrokePath(ctx);
break;
casekRectShape:
//填充矩形
CGContextFillRect(ctx ,[selfcurRect]);
break;
casekEllipseShape:
//填充橢圓
CGContextFillEllipseInRect(ctx ,[selfcurRect]);
break;
casekRoundRectShape:
//計(jì)算左上角的坐標(biāo)
leftTopX = firstTouch.x < lastTouch.x ? firstTouch.x :
lastTouch.x;
leftTopY = firstTouch.y < lastTouch.y ? firstTouch.y :
lastTouch.y;
//添加圓角矩形的路徑
CGContextAddRoundRect(ctx ,leftTopX ,leftTopY ,
fabs(lastTouch.x - firstTouch.x) ,
fabs(lastTouch.y - firstTouch.y) ,16);
//填充路徑
CGContextFillPath(ctx);
break;
casekPenShape:
//添加從prevTouch到lastTouch的路徑
CGContextMoveToPoint(ctx, prevTouch.x, prevTouch.y);
CGContextAddLineToPoint(ctx, lastTouch.x, lastTouch.y);
//繪制路徑
CGContextStrokePath(ctx);
//使用prevTouch保存當(dāng)前點(diǎn)
prevTouch = lastTouch;
break;
}
}
@end
控制器1: SHContext.h
//=================
//=============
控制器4:ViewController.h
//=================
//=============
//
//? ViewController.h
//? HandDraw(手繪)~demo
//
//? Created by石虎on 2017/8/15.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import
@interfaceViewController :UIViewController
//改變顏色
- (IBAction)changeColor:(UISegmentedControl*)sender;
//改變形狀
- (IBAction)changeShape:(UISegmentedControl*)sender;
@end
控制器4:ViewController.m
//=================
//=============
//
//? ViewController.m
//? HandDraw(手繪)~demo
//
//? Created by石虎on 2017/8/15.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import"ViewController.h"
#import"SHDrawView.h"
@interfaceViewController()
{
NSArray*colors;
}
@end
@implementationViewController
- (void)viewDidLoad
{
[superviewDidLoad];
self.view.backgroundColor= [UIColorwhiteColor];
colors= [NSArrayarrayWithObjects:
[UIColorredColor],[UIColorgreenColor],
[UIColorblueColor],[UIColoryellowColor],
[UIColorpurpleColor],[UIColorcyanColor],
[UIColorblackColor] ,nil];
}
- (IBAction)changeColor:(UISegmentedControl*)sender {
//根據(jù)用戶的選擇來修改FKDrawView的當(dāng)前顏色
((SHDrawView*)self.view).currentColor= [colorsobjectAtIndex:
sender.selectedSegmentIndex];
}
- (IBAction)changeShape:(UISegmentedControl*)sender
{
//修改FKDrawView控件的shape屬性
((SHDrawView*)self.view).shape= sender.selectedSegmentIndex;
}
@end
//===============
//=======
謝謝!!!