一:自定义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, );
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);
// 遍历所有线
NSInteger countOfLines = self.pointsOfAllLines.count;
// 取出每条线
for (NSInteger i = ; i < countOfLines; i ++) {
NSArray *pointsOfline = self.pointsOfAllLines[i];
NSInteger countOfPoints = pointsOfline.count;
//设置这条线的颜色
//取出对应线的颜色
UIColor *currentColor = self.colorsOfAllLines[i];
[currentColor set];
//遍历这条线里的两个点
for (NSInteger j = ; j < countOfPoints; j ++) {
CGPoint location = [pointsOfline[j] CGPointValue];
if (j == ) {
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

三:效果:

2016-1-9 Quartz框架的学习,写字板demo的更多相关文章

  1. 2016-1-8 Quartz框架的学习,多个气球上升的小动画

    // // BallonView.m // 气球上升的动画 // // Created by Mac on 16/1/8. // Copyright © 2016年 Mac. All rights r ...

  2. 2016-1-9 Quartz框架的学习,剪裁图片并设置边框

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  3. Quartz框架学习(1)—核心层次结构

    Quartz框架学习 Quartz(任务调度)框架的核心组件: job:任务.即任务调度行为中所要调度的对象. trigger:触发器.是什么促使了一个任务的调度?当然是时间.这也算事件驱动类型程序. ...

  4. Quartz框架(第一版)

    任务调度 在企业级应用中,经常会制定一些"计划任务",即在某个时间点做某件事情 核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作 任务调度涉及多线程并发.线程池维 ...

  5. Quartz框架

    Quartz框架 Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制.Quartz 允许开发人员根据时间间隔(或天)来调度作业.它实现了作业和触发器的多 ...

  6. spring整合Quartz框架过程,大家可以参考下

    这篇文章详细介绍了spring集成quartz框架流程,通过示例代码进行了详细说明,对学习或任务有参考学习价值,并可供需要的朋友参考. 1.quartz框架简介(m.0831jl.com) quart ...

  7. DBFlow框架的学习笔记之入门

    什么是DBFlow? dbflow是一款android高性的ORM数据库.可以使用在进行项目中有关数据库的操作.github下载源码 1.环境配置 先导入 apt plugin库到你的classpat ...

  8. 一起来学习Android自定义控件2-简单的写字板控件

    概述 上一篇文章我们对自定义控件进行了一个大体的知识介绍.今天就来学习自定义一个简单的写字板控件. 先来看看效果图 就是简单的根据手指写下的轨迹去画出内容 实现 在上一篇文章里提到了android官方 ...

  9. 【淘淘】Spring整合Quartz框架

    我在外面工作实习的时候,我们做的项目是一个日报子系统,也就是定时定点为公司生成一些报表数据还有一些数据反馈.这时候我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.当时,我做 ...

随机推荐

  1. linux笔记:压缩解压命令gzip,gunzip,tar,zip,unzip,bzip2,bunzip2

    命令名称:gzip功能:压缩文件命令所在路径:/bin/gzip用法:gzip 文件压缩后文件格式:.gz其他:压缩后不保留原文件:只能压缩文件,不能压缩目录 命令名称:gunzip功能:解压.gz格 ...

  2. java获取本月开始时间和结束时间、上个月第一天和最后一天的时间以及当前日期往前推一周、一个月

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.uti ...

  3. 求质数算法的N种境界[1] - 试除法和初级筛法

    ★引子 前天,俺在<俺的招聘经验[4]:通过笔试答题能看出啥?>一文,以"求质数"作为例子,介绍了一些考察应聘者的经验.由于本文没有政治敏感内容,顺便就转贴到俺在CSD ...

  4. 20150206读书笔记<深入理解计算机系统>

    ●第一章 C是系统级编程的首选.C++显示支持抽象,属于应用级程序设计语言. 简单例子: 一个典型系统的硬件组成: 存储器的层次结构: 注:存储器层次结构的设计思想是,该层存储器作为下一层存储器的高速 ...

  5. hdu----(5053)the Sum of Cube(签到题,水体)

    the Sum of Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  6. OC 继承子类对象调用方法机制 子类对象访问父类中的实例变量

    在继承中,子类对象如何调用到正确方法的机制 每一个Objective - C对象都有一个隐藏的指针指向类的代码,当向一个对象发送消息的时候,当前的对象会首先在当前类里去查找相应的方法,如果找到的话,直 ...

  7. Android 编程下的代码混淆

    什么是代码混淆 Java 是一种跨平台的.解释型语言,Java 源代码编译成中间”字节码”存储于 class 文件中.由于跨平台的需要,Java 字节码中包括了很多源代码信息,如变量名.方法名,并且通 ...

  8. JAVA读取EXCEL文件异常Unable to recognize OLE stream

    异常: jxl.read.biff.BiffException: Unable to recognize OLE stream at jxl.read.biff.CompoundFile.<in ...

  9. java使用dom4j解析xml文件

    关于xml的知识,及作用什么的就不说了,直接解释如何使用dom4j解析.假如有如下xml: dom4j解析xml其实很简单,只要你有点java基础,知道xml文件.结合下面的xml文件和java代码, ...

  10. ExtJs中实现tree节点,全部是单击展开和收缩效果,和收藏夹点击功能一样

    listeners : { click : function(node, c) {// 单击节点事件(node是节点对象) if(!node.isLeaf()){//不是叶子节点 node.singl ...