项目描述:本产品是一款绘图软件,专为儿童打造的画画应用.可充分发挥孩子们的创造力,让孩子们通过自己的小手在画布上尽情创作出自己的画作. 应用包括小画板,保存所画图像,分享画作等模块。
主要技术:使用手势捕捉路径;使用Quartz2D绘图框架绘制路径;
#import "ViewController.h"
#import "CView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet CView* cview;
@property (weak, nonatomic) IBOutlet UISlider* lineWidthView;
@property (weak, nonatomic) IBOutlet UIButton* firstButton;
@end
@implementation ViewController
// 清屏
- (IBAction)clear:(id)sender
{
[self.cview clear];
}
// 回退
- (IBAction)back:(id)sender
{
[self.cview back];
}
// 橡皮
- (IBAction)eraser:(id)sender
{
[self.cview eraser];
}
// 保存
- (IBAction)save:(id)sender
{
// 开启图片类型的图形上下文
UIGraphicsBeginImageContextWithOptions(self.czview.bounds.size, NO, 0);
// 获取当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 截图
[self.cview.layer renderInContext:ctx];
// 取图片
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
// 关闭图片类型的图形上下文
UIGraphicsEndImageContext();
// 保存到相册
UIImageWriteToSavedPhotosAlbum(image, NULL, NULL, NULL);
}
//// 监听线宽的变化
//- (IBAction)widthChange:(UISlider*)sender
//{
// // NSLog(@"%f", sender.value);
// self.cview.lineWidth = sender.value;
//}
// 颜色按钮的点击事件
- (IBAction)colorChange:(UIButton*)sender
{
self.cview.lineColor = sender.backgroundColor;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 设置默认线宽
self.cview.lineWidth = self.lineWidthView.value;
// 设置默认的颜色
[self colorChange:self.firstButton];
[self.cview setLineWidthBlock:^CGFloat {
return self.lineWidthView.value;
}];
}
@end
#import "CView.h"
@interface CBezierPath : UIBezierPath
@property (strong, nonatomic) UIColor* lineColors;
@end
@implementation CBezierPath
@end
@interface CView ()
//@property (strong, nonatomic) CBezierPath* path;
@property (strong, nonatomic) NSMutableArray* paths;
@end
@implementation CView
// 清屏
- (void)clear
{
[self.paths removeAllObjects];
// 重绘
[self setNeedsDisplay];
}
// 回退
- (void)back
{
[self.paths removeLastObject];
// 重绘
[self setNeedsDisplay];
}
// 橡皮
- (void)eraser
{
self.lineColor = self.backgroundColor;
}
// 懒加载 初始化
- (NSMutableArray*)paths
{
if (!_paths) {
_paths = [NSMutableArray array];
}
return _paths;
}
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
// 获取触摸对象
UITouch* t = touches.anyObject;
// 获取手指的位置
CGPoint p = [t locationInView:t.view];
// 创建路径
CBezierPath* path = [[CBezierPath alloc] init];
[path moveToPoint:p];
// 设置线宽
// [path setLineWidth:self.lineWidth];
[path setLineColors:self.lineColor];
if (self.lineWidthBlock) {
[path setLineWidth:self.lineWidthBlock()];
}
// 把路径添加到数组当中
[self.paths addObject:path];
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
// 获取触摸对象
UITouch* t = touches.anyObject;
// 获取手指的位置
CGPoint p = [t locationInView:t.view];
// 让数组中的最后一条路径进行连线
[[self.paths lastObject] addLineToPoint:p];
// 重绘
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
for (CBezierPath* path in self.paths) {
// 设置颜色
[path.lineColors set];
// 设置连接处的样式
[path setLineJoinStyle:kCGLineJoinRound];
// 设置头尾的样式
[path setLineCapStyle:kCGLineCapRound];
// 渲染
[path stroke];
}
}
@end
原文:http://www.cnblogs.com/donghaoios/p/5196325.html