一:自定义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. noip赛前小结2

    嗯...赛前的第二份小结. 总结一下应该做的几个事情就好了. (1)关于做题顺序 做题顺序是很重要的. 开始的时候先审题,看清数据范围什么的,随便想一想,大概估计一下自己的得分. 第二题再把每道题仔细 ...

  2. 你的java 代码对JIT编译友好吗?

    JIT编译器是Java虚拟机(以下简称JVM)中效率最高并且最重要的组成部分之一.但是很多的程序并没有充分利用JIT的高性能优化能力,很多开发者甚至也并不清楚他们的程序有效利用JIT的程度. 在本文中 ...

  3. jq--回到顶部

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. hdu-----(1151)Air Raid(最小覆盖路径)

    Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  5. 如何用SQL返回两个日期之间的所有连续日期

    在层次查询中,Oracle引入了一个伪列level,用来表示当前行(节点)对应的level, 它从1开始计数,每多一层level的值就加1. 我们可以据此实现对两个日期/整数之间所有日期/整数的遍历. ...

  6. linux shell中,单引号、 双引号,反引号(``),$()的区别

    一.单引号和双引号 首先,单引号和双引号,都是为了解决中间有空格的问题. 空格在linux中时作为一个很典型的分隔符,比如 string1=this is a string,这样执行会报错.为了避免这 ...

  7. [转]Perfmon - Windows 自带系统监测工具

    以下内容转自:http://blog.csdn.net/oscar999/article/details/7918385 ---------------------------分割线--------- ...

  8. oracle查锁表SQL

    SELECT l.session_id sid, s.serial#, l.locked_mode, l.oracle_username, s.user#, l.os_user_name,s.mach ...

  9. 安装apache+php+mysql

    #yum -y install httpd   安装完成之后查看安装时生成了哪些文件: #rpm -ql httpd   绑定域名(如果没有域名可以跳过): #cd /etc/httpd/conf.d ...

  10. tortoisegit教程

    tortoisegit教程: http://www.mamicode.com/info-detail-311565.html https://my.oschina.net/longxuu/blog/1 ...