2016-1-9 Quartz框架的学习,写字板demo

一:自定义view .h文件中代码如下

#import <UIKit/UIKit.h>

@interface ZLpaintView : UIView
@property(nonatomic, strong) UIColor *currentColor;
- (void)back;
- (void)clear;
- (void)savetoFile:(NSString *)file;

@end

.m中如下

#import "ZLpaintView.h"
@interface ZLpaintView()

//用于存放 存放某条线的点 的数组
@property (nonatomic, strong) NSMutableArray *pointsOfAllLines;
//用于存放每条线的颜色
@property (nonatomic, strong) NSMutableArray *colorsOfAllLines;

@end

@implementation ZLpaintView
- (NSMutableArray *)pointsOfAllLines
{
    if (!_pointsOfAllLines) {
        _pointsOfAllLines = [NSMutableArray array];
    }
    return _pointsOfAllLines;
}
- (NSMutableArray *)colorsOfAllLines
{
    if (!_colorsOfAllLines) {
        _colorsOfAllLines = [NSMutableArray array];
    }
    return _colorsOfAllLines;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
//    设置线宽和收尾及连接点的样式
    CGContextSetLineWidth(context, 8);
    CGContextSetLineCap(context,  kCGLineCapRound);
    CGContextSetLineJoin(context, kCGLineJoinRound);
//    遍历所有线
    NSInteger countOfLines = self.pointsOfAllLines.count;
    //        取出每条线
    for (NSInteger i = 0; i < countOfLines; i ++) {
      NSArray *pointsOfline =  self.pointsOfAllLines[i];
        NSInteger countOfPoints = pointsOfline.count;
        //设置这条线的颜色
         //取出对应线的颜色
        UIColor *currentColor = self.colorsOfAllLines[i];
        [currentColor set];
        //遍历这条线里的两个点
        for (NSInteger j = 0; j < countOfPoints; j ++) {
            CGPoint location = [pointsOfline[j] CGPointValue];
            if (j == 0) {
                CGContextMoveToPoint(context,location.x,location.y);
            }else{
                CGContextAddLineToPoint(context,location.x,location.y);
            }
        }
         CGContextStrokePath(context);//每画完一条线,渲染一次
    }
   
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//    一触摸屏幕,就建立一个数组用于存储一条线的点。
    NSMutableArray *pointsOfLine = [NSMutableArray array];
//  生成线后,将他的对应颜色也放进数组里
    if (!self.currentColor) {
        self.currentColor = [UIColor blackColor];//如果当前颜色为空,则设置黑色
        [self.colorsOfAllLines addObject:self.currentColor];
    }else
    {
        [self.colorsOfAllLines addObject:self.currentColor];//否则直接加入到这个数组中
    }
//    将生成的数组存放在自己属性数组中。
    [self.pointsOfAllLines addObject:pointsOfLine];
    
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
//    NSLog(@"%@",[NSValue valueWithCGPoint:location]);
    NSMutableArray * pointsOfLine = [self.pointsOfAllLines lastObject];
    [pointsOfLine addObject:[NSValue valueWithCGPoint:location]];
    NSLog(@"%@",pointsOfLine);
    [self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%ld",self.pointsOfAllLines.count);
}
- (void)back
{
    [self.pointsOfAllLines removeLastObject];
    [self setNeedsDisplay];
}
- (void)clear
{
    [self.pointsOfAllLines removeAllObjects];
    [self setNeedsDisplay];
}
- (void)savetoFile:(NSString *)file
{
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *imageDate = UIImagePNGRepresentation(newImage);
    [imageDate writeToFile:file atomically:YES];
}
@end

二:在storyboard拖相应控件,并在控制器中实现相应地方法,代码如下:

#import "ViewController.h"
#import "ZLpaintView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet ZLpaintView *paintView;
- (IBAction)backClick;
- (IBAction)clearClick;
- (IBAction)saveClick;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)backClick {
    [self.paintView back];
}

- (IBAction)clearClick {
    [self.paintView clear];
    NSLog(@"%@",self.paintView.currentColor);
}

- (IBAction)saveClick {
    [self.paintView savetoFile:@"/Users/mac/Desktop/TheImage.png"];
}
- (IBAction)colorBtnClick:(UIButton *)sender
{
    
//    设置当前的颜色
    self.paintView.currentColor = sender.backgroundColor;
}
@end

三:效果:

转载于:https://www.cnblogs.com/BJTUzhengli/p/5117202.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值