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, );
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的更多相关文章
- 2016-1-8 Quartz框架的学习,多个气球上升的小动画
// // BallonView.m // 气球上升的动画 // // Created by Mac on 16/1/8. // Copyright © 2016年 Mac. All rights r ...
- 2016-1-9 Quartz框架的学习,剪裁图片并设置边框
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- Quartz框架学习(1)—核心层次结构
Quartz框架学习 Quartz(任务调度)框架的核心组件: job:任务.即任务调度行为中所要调度的对象. trigger:触发器.是什么促使了一个任务的调度?当然是时间.这也算事件驱动类型程序. ...
- Quartz框架(第一版)
任务调度 在企业级应用中,经常会制定一些"计划任务",即在某个时间点做某件事情 核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作 任务调度涉及多线程并发.线程池维 ...
- Quartz框架
Quartz框架 Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制.Quartz 允许开发人员根据时间间隔(或天)来调度作业.它实现了作业和触发器的多 ...
- spring整合Quartz框架过程,大家可以参考下
这篇文章详细介绍了spring集成quartz框架流程,通过示例代码进行了详细说明,对学习或任务有参考学习价值,并可供需要的朋友参考. 1.quartz框架简介(m.0831jl.com) quart ...
- DBFlow框架的学习笔记之入门
什么是DBFlow? dbflow是一款android高性的ORM数据库.可以使用在进行项目中有关数据库的操作.github下载源码 1.环境配置 先导入 apt plugin库到你的classpat ...
- 一起来学习Android自定义控件2-简单的写字板控件
概述 上一篇文章我们对自定义控件进行了一个大体的知识介绍.今天就来学习自定义一个简单的写字板控件. 先来看看效果图 就是简单的根据手指写下的轨迹去画出内容 实现 在上一篇文章里提到了android官方 ...
- 【淘淘】Spring整合Quartz框架
我在外面工作实习的时候,我们做的项目是一个日报子系统,也就是定时定点为公司生成一些报表数据还有一些数据反馈.这时候我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.当时,我做 ...
随机推荐
- svn 大杂烩
svn : trunk 日常开发 branch 多版开发,或者修复bug,测试 tag 开发到一阶段打一个tag,给外部使用 属性externals:可以引用外部的公共工程.这个工程最好是稳定的,不 ...
- [转]用Python读写Excel文件
[转]用Python读写Excel文件 转自:http://www.gocalf.com/blog/python-read-write-excel.html#xlrd-xlwt 虽然天天跟数据打交 ...
- MySQL忘记密码怎么办
1. 关闭正在运行的MySQL服务2. 打开DOS窗口,转到mysql\bin目录3. 输入mysqld --skip-grant-tables 回车 --skip-grant-table ...
- abap注意
1.建表的时候所有的数据元素的总长度不能超过1024. 2.表的主键修改在se11激活不成功,但是可以在se11保存,然后到se14中激活. 3.SM12解锁,在很多时候,经常出现某个表或者可修改的地 ...
- DB、ETL、DW、OLAP、DM、BI关系结构图
DB.ETL.DW.OLAP.DM.BI关系结构图 在此大概用口水话简单叙述一下他们几个概念: (1)DB/Database/数据库——这里一般指的就是OLTP数据库,在线事物数据库,用来支持生产的, ...
- tab切换-自动、点击、内容变换
<div class="tab"> <ul class="pics"> ...
- http请求详解
GET GET方法意思是获取被请求URI(Request-URI)指定的信息(以实体的格式).如果请求URI涉及到一个数据生成过程,那么这个过程生成的数据应该被作为实体在响应中返回而不是过程的源文本, ...
- Angularjs学习——(一)
去年就接触过AngularJS吧,只可惜那时候仅仅是跟着“老大”机械的完成了“饿了么”——一个单页面的手机App,而其中的什么原理,怎样来实现,自己也是似懂非懂,直至今天自己再次拿起来它,并一个人来研 ...
- BZOJ3307 雨天的尾巴
首先考虑序列怎么做... 只要把操作差分了,记录在每个点上 然后维护一棵权值线段树,表示每个颜色出现的次数,支持单点修改和查询最大值操作 只要把序列扫一遍就好了,时间复杂度$O(n + m*logZ) ...
- 陈朱兴-js写法【案例】:
ajax请求: 一.从服务器端请求数据: var url = '';url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='+ ...