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框架
我在外面工作实习的时候,我们做的项目是一个日报子系统,也就是定时定点为公司生成一些报表数据还有一些数据反馈.这时候我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.当时,我做 ...
随机推荐
- Question store (Repeated review)
题目36 - ACM在线评测系统http://acm.nyist.net/JudgeOnline/problem.php?pid=36 用户名密码INVATION 讲道理太卡 第一:要注意不同的函数 ...
- Android虚拟机常见错误及解决办法
第一个: [2012-11-09 13:15:14 - Tesa] Android Launch! [2012-11-09 13:15:14 - Tesa] The connection to adb ...
- 两端对齐(兼容较好,支持IE)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Maven基础知识和环境搭建
基本概念和生命周期 Maven是现在流行的构建自动化工具,提供了软件构建过程中全生命周期的管理. 基础目录结构 基础目录结构如下: 根目录:存放pom.xml 和所有的子目录 ${basedir}/s ...
- Shell基础:Linux权限管理
Linux权限基本概念 查看系统(文件夹/文件)权限: ls -l =>d/- xxx xxx xxx. num owner group size date filename ...
- Linux下把Mysql和Apache加入到系统服务里
Linux下注册Apache与MySQL为系统服务 Apache加入到系统服务里面: cp /安装目录下/apache/bin/apachectl /etc/rc.d/init.d/httpd 修改h ...
- Android receiver
可以在代码文件中声明一个receiver,也可以在manifest中声明一个,前者中的receiver只有在该activity launch起来以后才会监听其所感兴趣的事件, 而如果在androidM ...
- DataTable常用操作
添加列和行的三种方法(转载) 原文地址:http://www.cnblogs.com/jRoger/articles/1887581.html DataTable tblDatas =new Data ...
- sql 语句 截取字符串的两种方案
方案一:使用内置的函数 SUBSTRING,CHARINDEX,LEN三个内置函数 理论: SUBSTRING语法 SUBSTRING ( value_expression , start_exp ...
- Objective-C:Block
Block是OC中一种与其它语言的语法区别较大的一种用法,需要注意: Block也叫代码段,它封装了一段代码,可以在任何时候执行: Block可以作为函数参数或者函数的返回值,而其本身又可以带输入参数 ...