(素材源码)猫猫学IOS(三十四)UI之Quartz2D画画板的实现
2015-05-06 11:07
681 查看
猫猫分享,必须精品
原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243?viewmode=contents
源码:http://download.csdn.net/detail/u013357243/8666923
![](http://img.blog.csdn.net/20150506105448973)
NYView.m
其他的可以在源码中自己看,这两个是最重要的。
原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址: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
其他的可以在源码中自己看,这两个是最重要的。
相关文章推荐
- (素材源码)猫猫学IOS(三十四)UI之Quartz2D画画板的实现
- 猫猫学IOS(三十四)UI之Quartz2D画画板的实现
- 猫猫学IOS(三十四)UI之Quartz2D画画板的实现
- 猫猫学IOS(三十四)UI之Quartz2D画画板的实现
- (素材源码)猫猫学IOS(三十五)UI之Quartz2D仿真支付宝手势解锁_代理获得密码。
- (素材源码)猫猫学IOS(三十五)UI之Quartz2D仿真支付宝手势解锁_代理获得密码。
- (素材源码)猫猫学IOS(三十五)UI之Quartz2D仿真支付宝手势解锁_代理获得密码。
- (素材源码)猫猫学IOS(二十九)UI之Quartz2D自定义下载控件
- (素材源码)猫猫学IOS(二十九)UI之Quartz2D自定义下载控件
- AJ学IOS(34)UI之Quartz2D画画板的实现
- (素材源码)猫猫学IOS(八)UI之热门_喜马拉雅UI实现-UIScrollView的使用
- (素材源码)猫猫学IOS(七)UI之UITextField代理事件_类似QQ登陆窗口的简单实现
- (素材源码)猫猫学IOS(八)UI之热门_喜马拉雅UI实现-UIScrollView的使用
- (素材源码)猫猫学IOS(七)UI之UITextField代理事件_类似QQ登陆窗口的简单实现
- (素材源码)猫猫学IOS(十六)UI之XIB自定义Cell实现团购UI
- (素材源码)猫猫学IOS(十六)UI之XIB自定义Cell实现团购UI
- (素材源码)猫猫学IOS(十八)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复
- (素材源码)猫猫学IOS(十七)UI之纯代码自定义Cell实现新浪微博UI
- (素材源码)猫猫学IOS(十八)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复
- (素材源码)猫猫学IOS(九)UI之UIScrollView代理触摸实现_图片缩放