#import "ViewController.h"
#import "CZPaintView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet CZPaintView *paintView;
@end
@implementation ViewController
//滑动slider事件
- (IBAction)sliderValueChanged:(id)sender
{
//设置线宽
self.paintView.lineWidth = self.slider.value;
}
//点击按钮 传入按钮颜色
- (IBAction)colorBtnClick:(UIButton *)sender
{
//设置线的颜色
self.paintView.lineColor = sender.backgroundColor;
}
//清屏
- (IBAction)clear:(id)sender {
[self.paintView clear];
}
//回退
- (IBAction)back:(id)sender {
[self.paintView back];
}
//橡皮擦
- (IBAction)eraser:(id)sender {
[self.paintView eraser];
}
//保存
- (IBAction)save:(id)sender
{
//1.开启一个图形上下文对象
UIGraphicsBeginImageContextWithOptions(self.paintView.frame.size, NO, 0.0);
//2.获取刚刚开启的图形上下文对象
CGContextRef ctx = UIGraphicsGetCurrentContext();
//3.通过view.layer的renderInContext的方法 把指定view的内容绘制到图形上下文中
[self.paintView.layer renderInContext:ctx];
//4.从图形上下文中获取图片
UIImage * getImage = UIGraphicsGetImageFromCurrentImageContext();
//5.结束图形上下文
UIGraphicsEndImageContext();
//6.保存图片
UIImageWriteToSavedPhotosAlbum(getImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
{
NSString * str = @"保存成功";
if(error)
{
str = @"保存失败";
}
NSLog(@"%@",str);
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//设置默认线宽
self.paintView.lineWidth = self.slider.value;
}
//隐藏状态栏
- (BOOL) prefersStatusBarHidden
{
return YES;
}
#import <UIKit/UIKit.h>
@interface CZPaintView : UIView
@property(nonatomic,assign)CGFloat lineWidth;
@property (nonatomic, strong) UIColor *lineColor;
//清屏
- (void)clear;
//回退
- (void) back;
//橡皮擦
- (void) eraser;
@end
#import "CZPaintView.h"
#import "CZBezierPath.h"
@interface CZPaintView()
//@property (nonatomic, strong) UIBezierPath * path;
@property (nonatomic, strong) NSMutableArray *paths;
@end
@implementation CZPaintView
//懒加载
- (NSMutableArray *)paths
{
if(_paths == nil)
{
_paths = [NSMutableArray array];
}
return _paths;
}
//实现清屏方法
- (void)clear
{
//删除数组中的路径
[self.paths removeAllObjects];
//执行重绘
[self setNeedsDisplay];
}
//实现回退方法
- (void)back
{
//删除数组中的最后一个路径
[self.paths removeLastObject];
//执行重绘
[self setNeedsDisplay];
}
//实现橡皮擦功能
- (void)eraser
{
//设置线的颜色
self.lineColor = self.backgroundColor;
}
//手指按下
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//1.获取触摸对象
UITouch * touch = touches.anyObject;
//2.获取触摸点
CGPoint locP = [touch locationInView:touch.view];
//3.创建绘图路径
CZBezierPath * path = [[CZBezierPath alloc] init];
//3.1设置线宽
path.lineWidth = self.lineWidth;
//3.2设置颜色
path.lineColor = self.lineColor;
//4.添加子路径
[path moveToPoint:locP];
//5.把path添加到数组中
[self.paths addObject:path];
}
//手指移动
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//1.获取触摸对象
UITouch * touch = touches.anyObject;
//2.获取触摸点
CGPoint locP = [touch locationInView:touch.view];
//3.添加子路径
[[self.paths lastObject] addLineToPoint:locP];
//执行重绘
[self setNeedsDisplay];
}
//手指抬起
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
}
- (void)drawRect:(CGRect)rect {
// Drawing code
//绘图
for (CZBezierPath * path in self.paths)
{
//设置颜色
[path.lineColor set];
//设置线头样式
path.lineCapStyle = kCGLineCapRound;
//设置连接处的样式
path.lineJoinStyle = kCGLineJoinRound;
[path stroke];
}
}
@end