(素材源码)猫猫学IOS(三十四)UI之Quartz2D画画板的实现
来源:互联网 发布:saas招聘软件 编辑:程序博客网 时间:2024/05/01 15:44
猫猫分享,必须精品
原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243?viewmode=contents
源码:http://download.csdn.net/detail/u013357243/8666923
效果:
代码:
NYView
NYView.h
//// NYView.h// 画画板//// Created by apple on 15-5-6.// Copyright (c) 2015年 znycat. All rights reserved.//#import <UIKit/UIKit.h>@interface NYView : UIView- (void)clearView;- (void)backView;@end
NYView.m
//// NYView.m// 画画板//// Created by apple on 15-5-6.// Copyright (c) 2015年 znycat. All rights reserved.//#import "NYView.h"@interface NYView ()@property (nonatomic, strong) NSMutableArray *paths;@end@implementation NYView- (NSMutableArray *)paths{ if (_paths == nil) { _paths = [NSMutableArray array]; } return _paths;}// 开始触摸- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ // 1.获取手指对应UITouch对象 UITouch *touch = [touches anyObject]; // 2.通过UITouch对象获取手指触摸的位置 CGPoint startPoint = [touch locationInView:touch.view]; // 3.当用户手指按下的时候创建一条路径 UIBezierPath *path = [UIBezierPath bezierPath]; // 3.1设置路径的相关属性 [path setLineJoinStyle:kCGLineJoinRound]; [path setLineCapStyle:kCGLineCapRound]; [path setLineWidth:10]; // 4.设置当前路径的起点 [path moveToPoint:startPoint]; // 5.将路径添加到数组中 [self.paths addObject:path];}// 移动- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ // 1.获取手指对应UITouch对象 UITouch *touch = [touches anyObject]; // 2.通过UITouch对象获取手指触摸的位置 CGPoint movePoint = [touch locationInView:touch.view]; // 3.取出当前的path UIBezierPath *currentPaht = [self.paths lastObject]; // 4.设置当前路径的终点 [currentPaht addLineToPoint:movePoint]; // 6.调用drawRect方法重回视图 [self setNeedsDisplay];}// 离开view(停止触摸)- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ [self touchesMoved:touches withEvent:event]; /* // 1.获取手指对应UITouch对象 UITouch *touch = [touches anyObject]; // 2.通过UITouch对象获取手指触摸的位置 CGPoint endPoint = [touch locationInView:touch.view]; // 3.取出当前的path UIBezierPath *currentPaht = [self.paths lastObject]; // 4.设置当前路径的终点 [currentPaht addLineToPoint:endPoint]; // 6.调用drawRect方法重回视图 [self setNeedsDisplay]; */}// 画线- (void)drawRect:(CGRect)rect{ [[UIColor redColor] set]; // 边路数组绘制所有的线段 for (UIBezierPath *path in self.paths) { [path stroke]; }}- (void)clearView{ [self.paths removeAllObjects]; [self setNeedsDisplay];}- (void)backView{ [self.paths removeLastObject]; [self setNeedsDisplay];}@end
NYViewController
//// NYViewController.m// 画画板//// Created by apple on 15-5-6.// Copyright (c) 2015年 znycat. All rights reserved.//#import "NYViewController.h"#import "NYView.h"#import "MBProgressHUD+NJ.h"#import "UIImage+captureView.h"@interface NYViewController ()/** * 清屏 */- (IBAction)clearBtnClick;/** * 回退 */- (IBAction)backBtnClick;/** * 保存 */- (IBAction)saveBtnClick;@property (weak, nonatomic) IBOutlet NYView *customView;@end@implementation NYViewController- (IBAction)clearBtnClick { [self.customView clearView];}- (IBAction)backBtnClick { [self.customView backView];}- (IBAction)saveBtnClick { UIImage *newImage = [UIImage captureImageWithView:self.customView]; // 4.保存到相册 UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);}- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ if (error) { [MBProgressHUD showError:@"保存失败"]; }else { [MBProgressHUD showSuccess:@"保存成功"]; }}@end
其他的可以在源码中自己看,这两个是最重要的。
13 2
- (素材源码)猫猫学IOS(三十四)UI之Quartz2D画画板的实现
- 猫猫学IOS(三十四)UI之Quartz2D画画板的实现
- 猫猫学IOS(三十四)UI之Quartz2D画画板的实现
- AJ学IOS(34)UI之Quartz2D画画板的实现
- (素材源码)猫猫学IOS(二十九)UI之Quartz2D自定义下载控件
- (素材源码)猫猫学IOS(三十五)UI之Quartz2D仿真支付宝手势解锁_代理获得密码。
- (素材源码)猫猫学IOS(三十五)UI之Quartz2D仿真支付宝手势解锁_代理获得密码。
- (素材源码)猫猫学IOS(八)UI之热门_喜马拉雅UI实现-UIScrollView的使用
- (素材源码)猫猫学IOS(十六)UI之XIB自定义Cell实现团购UI
- (素材源码)猫猫学IOS(七)UI之UITextField代理事件_类似QQ登陆窗口的简单实现
- (素材源码)猫猫学IOS(九)UI之UIScrollView代理触摸实现_图片缩放
- (素材源码)猫猫学IOS(十七)UI之纯代码自定义Cell实现新浪微博UI
- (素材源码)猫猫学IOS(十五)UI之曾经大热的打砖块小游戏
- (素材源码) 猫猫学IOS(五)UI之360等下载管理器九宫格UI
- (素材源码)猫猫学IOS(六)UI之iOS热门游戏_超级猜图
- (素材源码)猫猫学IOS(二十六)UI之iOS抽屉效果小Demo
- (素材源码)猫猫学IOS(十四)UI之UITableView扩充_表格的修改_(增删移动)
- (素材源码)猫猫学IOS(十)UI之_NSTimer_ios计时器
- 基于webrtc的点对点多摄像头视频监控demo
- 关于win 8系统安装mysql报2502、2503错误解决方法
- mybatis foreach 强大
- java实现单向链
- 怎样反编译 Android APK?
- (素材源码)猫猫学IOS(三十四)UI之Quartz2D画画板的实现
- 冒泡排序和快速排序
- Linux下反弹shell方法
- HDU ACM 4500 小Q系列故事——屌丝的逆袭
- PhoneGap 跨平台应用:HealthWorking (2) 通过REST Service与服务器交互实现登陆
- function
- 正则表达式
- java中static关键字和final关键字
- NSLog用法,打印日志