iOS 手势绘图/小画板的实现

#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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值