一:自定义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. 【CodeForces 651A】Joysticks 模拟

    题意:给定a,b,每个单位时间可以将a,b中一台加1,一台减2,求最久可以支持多久. #include <cstdio> #include <algorithm> using ...

  2. 扒一扒JavaScript 预解释

    带var关键字预解释 让我们先看下这段代码执行的结果: 代码如下: alert(n);//弹出undefinedvar n = 10; 弹出的结果是undefined,为何不是10?让我们再看下面这段 ...

  3. robot API笔记5

    实现了 Libdoc 工具. 命令行Libdoc入口点和编程接口 提供的是单独的吗 robot.libdoc 模块. 这个包被认为是稳定的但不是公共API的一部分. robot.libdocpkg.L ...

  4. 51nod 1613翻硬币

    题目链接:51nod 1613 翻硬币 知乎上的理论解法http://www.zhihu.com/question/26570175/answer/33312310 本题精髓在于奇偶性讨论. 若 n ...

  5. Generic泛型

    1.问题 未使用泛型时,元素的类型不安全:操作麻烦,可能需要强制转换import java.util.ArrayList;import java.util.List;import org.junit. ...

  6. 最原始的COM组件调用过程(不使用注册表信息)

    最原始的COM组件调用过程(不使用注册表信息) 最近因为项目的关系开始研究COM组件了,以前都认为COM过时了,所以也没怎么接触. 现在好好补补课了. 一般调用COM都是通过注册表找到它的位置, 然后 ...

  7. C#学习笔记思维导图 一本书22张图

    阅读的书是<21天学通C#>博客中有下载 看看总结之后的模块 全部文件 初步展示 数据存储 继承模块 暂时就这些吧 全部思维导图22张打包下载

  8. 如何在Hadoop的MapReduce程序中处理JSON文件

    简介: 最近在写MapReduce程序处理日志时,需要解析JSON配置文件,简化Java程序和处理逻辑.但是Hadoop本身似乎没有内置对JSON文件的解析功能,我们不得不求助于第三方JSON工具包. ...

  9. cstring 的重载

    #include <iostream> #include <windows.h> using namespace std; +; class MyString { public ...

  10. 保护WIFI无线网络的安全

    本篇博客属于我们隐私与安全小贴士系列博客的一部分,其目的是确保您以及您的家人的上网安全.隐私与安全问题无论对我们还是对您都至关重要.我们在“不可 不知的小知识”网站上为您提供了如何安全,便捷地使用互联 ...