[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 ...
随机推荐
- gsp序列模式挖掘
数据挖掘进阶之序列模式挖掘GSP算法 绪 继续数据挖掘方面算法的讲解,前面讲解了数据挖掘中关联规则算法FP-Growth的实现.此篇博文主要讲解基于有趣性度量标准的GSP序列模式挖掘算法.有关论文后期 ...
- poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)
题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...
- OpenSSL再爆多处高危漏洞
OpenSSL团队于北京时间6月5号晚8点左右发布了5个安全补丁,这次的更新涉及多处高危漏洞,连接:http://www.openssl.org/news/ 受影响的版本包括: OpenSSL 1.0 ...
- 待实践三:MVC3下 路由的测试 使用 RouteDebug.dll 来测试判断路由是否符合
在需要进行测试路由是否匹配的项目中引用 RouteDebug.dll 并且在MVC的Global.asax里面加入一段代码 //下面这行代码一定是在 RegisterRoutes(Rou ...
- CentOS 5.6 netInstall可以的在线安装方式。
之前百度google了一把, 发现原来的地址都失效了. 只找到一个能用的. 下载9M多的CentOS Net Install ISO文件, 选择安装方式时选HTTP. 然后在后面的 服务器位置处输入 ...
- HDU 2444 The Accomodation of Students (偶图判定,匈牙利算法)
题意: 有一堆的学生关系,要将他们先分成两个组,同组的人都不互不认识,如果不能分2组,输出No.若能,则继续.在两组中挑两个认识的人(每组各1人)到一个双人房.输出需要多少个双人房? 思路: 先判定是 ...
- Java [Leetcode 260]Single Number III
题目描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...
- 一些网站的Android客户端
实际上就是浏览器(WebView),外面包装上了用户体验更好的外壳
- POJ 2549 Sumsets
Sumsets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10593 Accepted: 2890 Descript ...
- ArcGIS AO开发高亮显示某些要素
参考代码1 ifeaturecursor pcur = ifeatureclass.search(iqueryfilter pfilter); pfilter.whereclause = strAdd ...