[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 ...
随机推荐
- git中手动删除的文件如何在git中删除
在日常开发中,我们可能或手动删除(delete键删除的)一些文件,然而我们本来应该是用git rm fileName命令删除的,但是现在我们手动删除了,那么要如何在git里面讲那些手动删除的文件删除呢 ...
- 第六届华为创新杯编程大赛-进阶1第1轮 洞穴逃生 (bfs + 优先队列)
这个题容易出错想了挺长时间,然后代码不长,1Y.. 做完题,看了一下别人的博客,也可以优先用 闪烁法术, 在闪烁法术不不如跑步的阶段(即魔法恢复的时候)用跑步. 洞穴逃生 描述: 精灵王子爱好冒险,在 ...
- tomcat发布web service教程
这几天一直在准备找工作,自学了关于web service的一些基本的内容,也遇到了不少问题.现在就把我自己学到的知识和大家分享一下,由于是初学,所以有什么错误的地方请大家帮忙指正,感激不尽~~!! 1 ...
- 栈中的push实现
- 实际举例C#引用类型和值类型的区别<网摘>
我们都知道,c#的两大数据类型分别为值类型和引用类型.很多人或许闭着眼睛都能说出值类型包括简单类型.结构体类型和枚举类型,引用类型包括自定义类.数组.接口.委托等,但是当被问及到二者之间的联系和区别, ...
- word编辑器解码集合
$(document).ready(function () { $(".content").each(function () { var content = $(this).htm ...
- 服务器发布MVC常见问题解决方案
1 问题:IIS上部署MVC网站,打开后500错误:处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelin ...
- jquery图表插件morris.js参数详解和highcharts图表插件
一.morris.js 优点:轻巧.简单好用 缺点:没highcharts功能多,常用的足以 网址:http://morrisjs.github.io/morris.js/ 核心代码 1.head调用 ...
- Liunx常用的特殊环境变量
[weiqiang.liu@l~]$ sh variable xiaoqiang xiaoxuenumber:2scname:variablefirst:xiaoqiangsecond:xiaoxue ...
- NSIS 2.0界面快速入门
NSIS 2.0 版本支持定制的用户界面.所谓的 Modern UI(下称 MUI) 就是一种模仿最新的 Windows 界面风格的界面系统.MUI 改变了 NSIS 脚本的编写习惯,它使用 NSIS ...