[iOS UI进阶 - 4.0] 涂鸦app Demo


//
// PaintView.m
// PaintDemo
//
// Created by hellovoidworld on 15/1/10.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "PaintView.h"
#import "UIImage+Extension.h" @interface PaintView() @end @implementation PaintView - (NSMutableArray *)lines {
if (nil == _lines) {
_lines = [NSMutableArray array];
}
return _lines;
} #pragma mark - 触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint startLocation = [touch locationInView:touch.view]; // 开启一条新的线
NSMutableArray *points = [NSMutableArray array];
// 存储点信息到线上
[points addObject:[NSValue valueWithCGPoint:startLocation]];
// 存储到线组上
[self.lines addObject:points]; // 重绘
[self setNeedsDisplay];
} - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:touch.view]; // 拿到正在画的线
NSMutableArray *points = [self.lines lastObject];
// 添加点信息
[points addObject:[NSValue valueWithCGPoint:location]]; // 重绘
[self setNeedsDisplay];
} - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// 停止拖曳的逻辑其实和拖曳中是一样的
[self touchesMoved:touches withEvent:event];
} #pragma mark - 绘图方法
/** 绘图 */
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 遍历线组,把所有线画出来
for (NSArray *line in self.lines) {
for (int i=; i<line.count; i++) {
NSValue *pointValue = line[i];
CGPoint point = [pointValue CGPointValue]; // 如果是线的第一个点,要先移动画笔到那个点
if ( == i) {
CGContextMoveToPoint(ctx, point.x, point.y);
} else {
CGContextAddLineToPoint(ctx, point.x, point.y);
}
}
} // 设置线宽、线头样式、线转折样式
CGContextSetLineWidth(ctx, );
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineJoin(ctx, kCGLineJoinRound); // 渲染
CGContextStrokePath(ctx);
} #pragma mark - view操作方法
/** 回退 */
- (void)rollback {
[self.lines removeLastObject];
[self setNeedsDisplay];
} /** 清屏 */
- (void)clearScreen {
[self.lines removeAllObjects];
[self setNeedsDisplay];
} /** 保存 */
- (void)save {
// 1.获取图片
UIImage *image = [UIImage imageOfView:self]; // 2.保存图片到相册
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
} /** 保存图片后激发事件
* 这是文档推荐的方法
*/
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
NSLog(@"保存失败");
} else {
NSLog(@"保存成功");
}
} @end
//
// UIImage+Extension.m
// PaintDemo
//
// Created by hellovoidworld on 15/1/11.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "UIImage+Extension.h" @implementation UIImage(Extension) + (UIImage *) imageOfView:(UIView *) view {
// 1.开启图片上下文
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0); // 2.将view的layer渲染到上下文
[view.layer renderInContext:UIGraphicsGetCurrentContext()]; // 3.获取上下文中的图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); // 4.关闭图片上下文
UIGraphicsEndImageContext(); return image;
} @end
//
// BezierPaintView.m
// PaintDemo
//
// Created by hellovoidworld on 15/1/11.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "BezierPaintView.h"
#import "UIImage+Extension.h" @implementation BezierPaintView - (NSMutableArray *)lines {
if (nil == _lines) {
_lines = [NSMutableArray array];
}
return _lines;
} #pragma mark - 触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint startLocation = [touch locationInView:touch.view]; // 新建一条Bezier线
UIBezierPath *path = [UIBezierPath bezierPath];
[path setLineWidth:5.0];
[path setLineCapStyle:kCGLineCapRound];
[path setLineJoinStyle:kCGLineJoinRound]; // 移动到始点
[path moveToPoint:startLocation];
// 添加Bezier线到数组
[self.lines addObject:path]; // 重绘
[self setNeedsDisplay];
} - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:touch.view]; // 获得正在画的线
UIBezierPath *path = [self.lines lastObject];
// 画线-添加点信息
[path addLineToPoint:location]; // 重绘
[self setNeedsDisplay];
} - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// 停止拖曳的逻辑其实和拖曳中是一样的
[self touchesMoved:touches withEvent:event];
} #pragma mark - 绘图方法
/** 绘图 */
- (void)drawRect:(CGRect)rect { // 画出所有的线
for (UIBezierPath *path in self.lines) {
// 渲染
[path stroke];
} } #pragma mark - view操作方法
/** 回退 */
- (void)rollback {
[self.lines removeLastObject];
[self setNeedsDisplay];
} /** 清屏 */
- (void)clearScreen {
[self.lines removeAllObjects];
[self setNeedsDisplay];
} /** 保存 */
- (void)save {
// 1.获取图片
UIImage *image = [UIImage imageOfView:self]; // 2.保存图片到相册
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
} /** 保存图片后激发事件
* 这是文档推荐的方法
*/
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
NSLog(@"保存失败");
} else {
NSLog(@"保存成功");
}
} @end
- slider控件调整线宽
- 选择颜色

- 使用UIView作为色块
- 自定义一个继承UIView的类,作为色块class,给色块UIView加上点击事件
- 给色块UIView创建一个颜色属性和代理协议,代理是ViewController;创建一个点击代理事件方法
//
// ColorSelectionView.h
// PaintDemo
//
// Created by hellovoidworld on 15/1/11.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <UIKit/UIKit.h> @protocol ColorSelectionViewDelegate <NSObject> /** “色块”点击代理方法 */
@optional
- (void) selectColor:(UIColor *) selectedColor; @end @interface ColorSelectionView : UIView /** 代理 */
@property(nonatomic, strong) id<ColorSelectionViewDelegate> delegate; @end //
// ColorSelectionView.m
// PaintDemo
//
// Created by hellovoidworld on 15/1/11.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "ColorSelectionView.h" @implementation ColorSelectionView - (void)awakeFromNib {
// 给UIView设置点击事件
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(colorClicked)];
[self addGestureRecognizer:tapGesture];
} /** “色块”点击事件 */
- (void) colorClicked {
[self.delegate selectColor:self.backgroundColor];
} @end
//
// HVWBezierPath.h
// PaintDemo
//
// Created by hellovoidworld on 15/1/11.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <UIKit/UIKit.h> @interface HVWBezierPath : UIBezierPath /** 颜色 */
@property(nonatomic, strong) UIColor *color; @end
- 拖入所有“色块”对象到ViewController,设置代理
- 遵守“色块”协议,实现代理方法
//
// ViewController.m
// PaintDemo
//
// Created by hellovoidworld on 15/1/10.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "ViewController.h"
#import "BezierPaintView.h"
#import "ColorSelectionView.h" @interface ViewController () <ColorSelectionViewDelegate> @property (weak, nonatomic) IBOutlet BezierPaintView *paintView; /** 颜色选择集合 */
@property (strong, nonatomic) IBOutletCollection(ColorSelectionView) NSArray *colorSelection; - (IBAction)rollback;
- (IBAction)clearScreen;
- (IBAction)save;
- (IBAction)lineWidthChange:(UISlider *)sender; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 设置颜色选择器的代理
for (ColorSelectionView *colorView in self.colorSelection) {
colorView.delegate = self;
}
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /** 回退 */
- (IBAction)rollback {
[self.paintView rollback];
} /** 清屏 */
- (IBAction)clearScreen {
[self.paintView clearScreen];
} /** 保存 */
- (IBAction)save {
[self.paintView save];
} /** 改变线粗 */
- (IBAction)lineWidthChange:(UISlider *)sender {
self.paintView.lineWidth = sender.value;
} #pragma mark - ColorSelectionViewDelegate 代理方法
- (void) selectColor:(UIColor *) selectedColor {
self.paintView.lineColor = selectedColor;
}
@end
//
// BezierPaintView.m
// PaintDemo
//
// Created by hellovoidworld on 15/1/11.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "BezierPaintView.h"
#import "UIImage+Extension.h"
#import "HVWBezierPath.h" @interface BezierPaintView() @end @implementation BezierPaintView - (NSMutableArray *)lines {
if (nil == _lines) {
_lines = [NSMutableArray array];
}
return _lines;
} #pragma mark - 触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint startLocation = [touch locationInView:touch.view]; // 新建一条Bezier线
HVWBezierPath *path = [[HVWBezierPath alloc] init]; // 配置线粗
if (self.lineWidth) {
[path setLineWidth:self.lineWidth];
} // 配置线色
if (self.lineColor) {
path.color = self.lineColor;
} [path setLineCapStyle:kCGLineCapRound];
[path setLineJoinStyle:kCGLineJoinRound]; // 移动到始点
[path moveToPoint:startLocation];
// 添加Bezier线到数组
[self.lines addObject:path]; // 重绘
[self setNeedsDisplay];
} - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:touch.view]; // 获得正在画的线
HVWBezierPath *path = [self.lines lastObject];
// 画线-添加点信息
[path addLineToPoint:location]; // 重绘
[self setNeedsDisplay];
} - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// 停止拖曳的逻辑其实和拖曳中是一样的
[self touchesMoved:touches withEvent:event];
} #pragma mark - 绘图方法
/** 绘图 */
- (void)drawRect:(CGRect)rect { // 画出所有的线
for (HVWBezierPath *path in self.lines) { // 设置颜色
if (path.color) {
[path.color set];
} // 渲染
[path stroke];
} } #pragma mark - view操作方法
/** 回退 */
- (void)rollback {
[self.lines removeLastObject];
[self setNeedsDisplay];
} /** 清屏 */
- (void)clearScreen {
[self.lines removeAllObjects];
[self setNeedsDisplay];
} /** 保存 */
- (void)save {
// 1.获取图片
UIImage *image = [UIImage imageOfView:self]; // 2.保存图片到相册
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
} /** 保存图片后激发事件
* 这是文档推荐的方法
*/
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
NSLog(@"保存失败");
} else {
NSLog(@"保存成功");
}
} @end
[iOS UI进阶 - 4.0] 涂鸦app Demo的更多相关文章
- [iOS UI进阶 - 5.0] 手势解锁Demo
A.需求 1.九宫格手势解锁 2.使用了绘图和手势事件 code source: https://github.com/hellovoidworld/GestureUnlockDemo B ...
- [iOS UI进阶 - 2.0] 彩票Demo v1.0
A.需求 1.模仿“网易彩票”做出有5个导航页面和相应功能的Demo 2.v1.0 版本搭建基本框架 code source:https://github.com/hellovoidworld/H ...
- [iOS UI进阶 - 3.0] 触摸事件的基本处理
A.需要掌握和练习的 1.介绍事件类型2.通过按钮的事件处理引出view的事件处理3.响应者对象 --> UIResponder --> UIView4.view的拖拽* 实现触摸方法,打 ...
- iOS UI进阶-4.0 地图与定位
在移动互联网时代,移动app能解决用户的很多生活琐事,比如 导航:去任意陌生的地方 周边:找餐馆.找酒店.找银行.找电影院 在上述应用中,都用到了地图和定位功能,在iOS开发中,要想加入这2大功能 ...
- iOS UI进阶-1.0 Quartz2D
概述 Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统.Quartz 2D能完成的工作: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成PDF ...
- [iOS UI进阶 - 6.0] CALayer
A.基本知识 1.需要掌握的 CALayer的基本属性 CALayer和UIView的关系 position和anchorPoint的作用 2.概念 在iOS中,你能看得见摸得着的东西基本上都是U ...
- iOS UI进阶-3.0 核心动画
Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对应的框架<Quar ...
- iOS UI进阶-2.0 CALayer
在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView 其实UIView之所以能显示在屏幕上,完全是因为它内部的一个图 ...
- iOS UI进阶-6.0 手势
给每个页面添加手势,只需要统一设置不是根控制器的页面,都增加手势.需要自定义导航控制器 1.继承代理 @interface BSNavigationController ()<UIGesture ...
随机推荐
- java8 十大新特性
这篇文章是对Java 8中即将到来的改进做一个面向开发者的综合性的总结,JDK的这一特性将会在2013年9月份发布. 在写这篇文章的时候,Java 8的开发工作仍然在紧张有序的进行中,语言特新和API ...
- 第六届华为创新杯编程大赛-进阶1第1轮 洞穴逃生 (bfs + 优先队列)
这个题容易出错想了挺长时间,然后代码不长,1Y.. 做完题,看了一下别人的博客,也可以优先用 闪烁法术, 在闪烁法术不不如跑步的阶段(即魔法恢复的时候)用跑步. 洞穴逃生 描述: 精灵王子爱好冒险,在 ...
- UVa 12186 Another Crisis
题意: 给出一个树状关系图,公司里只有一个老板编号为0,其他人员从1开始编号.除了老板,每个人都有一个直接上司,没有下属的员工成为工人. 工人们想写一份加工资的请愿书,只有当不少于员工的所有下属的T% ...
- Asp.Net读写XML简单方法
xml文件 <?xml version="1.0" encoding="utf-8"?> <book> <title>web ...
- Android 系统内置App JNI
说明 将Android应用作为系统内置遇到一些问题: 一个是使用Android源码的mmm命令生成的JNI名字和使用NDK生成的JNI的名字是不一样的: 另外就是AndroidManifest.xml ...
- javaScript的函数(Function)对象的声明(@包括函数声明和函数表达式)
写作缘由: 平时再用js写函数的时候,一般都是以惯例 function fn () {} 的方式来声明一个函数,在阅读一些优秀插件的时候又不免见到 var fn = function () {} 这种 ...
- 【转】./a.out 2>&1 > outfile
原文网址:http://www.cnblogs.com/zhaoyl/archive/2012/10/22/2733418.html APUE 3.5关于重定向有个容易迷惑人的问题: ./a.out ...
- Egret应用开发实践(01) Egret与WebPack
Egret Egret引擎是一款使用TypeScript语言构建的开源免费的移动游戏引擎.Egret仅是纯粹的使用TypeScript语言来开发游戏,开发后可以使用Egret来打包为HTML5网页游戏 ...
- Ejabberd源码解析前奏--管理
一.ejabberdctl 使用ejabberdctl命令行管理脚本,你可以执行ejabberdctl命令和一些普通的ejabberd命令(后面会详细解说).这意味着你可以在一个本地或远程ejabbe ...
- 零基础编程指南(By Turtle)
[零.基础] 1.看文章:<程序猿搜索的技巧>(未完成) [一.入门] 学习语言:VB 安装:下载VB6即可 教程:<李天生vb从入门到精通>http://www.xin372 ...