博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 手势绘图/小画板的实现
阅读量:4290 次
发布时间:2019-05-27

本文共 3745 字,大约阅读时间需要 12 分钟。

#import "ViewController.h"#import "CZPaintView.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UISlider *slider;@property (weak, nonatomic) IBOutlet CZPaintView *paintView;@end@implementation ViewController//滑动slider事件- (IBAction)sliderValueChanged:(id)sender{    //设置线宽    self.paintView.lineWidth = self.slider.value;}//点击按钮 传入按钮颜色- (IBAction)colorBtnClick:(UIButton *)sender{    //设置线的颜色    self.paintView.lineColor = sender.backgroundColor;}//清屏- (IBAction)clear:(id)sender {    [self.paintView clear];}//回退- (IBAction)back:(id)sender {    [self.paintView back];}//橡皮擦- (IBAction)eraser:(id)sender {    [self.paintView eraser];}//保存- (IBAction)save:(id)sender{    //1.开启一个图形上下文对象    UIGraphicsBeginImageContextWithOptions(self.paintView.frame.size, NO, 0.0);        //2.获取刚刚开启的图形上下文对象    CGContextRef ctx = UIGraphicsGetCurrentContext();        //3.通过view.layer的renderInContext的方法 把指定view的内容绘制到图形上下文中    [self.paintView.layer renderInContext:ctx];        //4.从图形上下文中获取图片    UIImage * getImage = UIGraphicsGetImageFromCurrentImageContext();        //5.结束图形上下文    UIGraphicsEndImageContext();        //6.保存图片    UIImageWriteToSavedPhotosAlbum(getImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);    }- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;{    NSString * str = @"保存成功";    if(error)    {        str = @"保存失败";    }    NSLog(@"%@",str);}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        //设置默认线宽    self.paintView.lineWidth = self.slider.value;}//隐藏状态栏- (BOOL) prefersStatusBarHidden{    return YES;}
#import 
@interface CZPaintView : UIView@property(nonatomic,assign)CGFloat lineWidth;@property (nonatomic, strong) UIColor *lineColor;//清屏- (void)clear;//回退- (void) back;//橡皮擦- (void) eraser;@end

#import "CZPaintView.h"#import "CZBezierPath.h"@interface CZPaintView()//@property (nonatomic, strong) UIBezierPath * path;@property (nonatomic, strong) NSMutableArray *paths;@end@implementation CZPaintView//懒加载- (NSMutableArray *)paths{    if(_paths == nil)    {        _paths = [NSMutableArray array];    }    return _paths;}//实现清屏方法- (void)clear{    //删除数组中的路径    [self.paths removeAllObjects];    //执行重绘    [self setNeedsDisplay];}//实现回退方法- (void)back{    //删除数组中的最后一个路径    [self.paths removeLastObject];        //执行重绘    [self setNeedsDisplay];    }//实现橡皮擦功能- (void)eraser{    //设置线的颜色    self.lineColor = self.backgroundColor;}//手指按下- (void)touchesBegan:(NSSet
*)touches withEvent:(UIEvent *)event{ //1.获取触摸对象 UITouch * touch = touches.anyObject; //2.获取触摸点 CGPoint locP = [touch locationInView:touch.view]; //3.创建绘图路径 CZBezierPath * path = [[CZBezierPath alloc] init]; //3.1设置线宽 path.lineWidth = self.lineWidth; //3.2设置颜色 path.lineColor = self.lineColor; //4.添加子路径 [path moveToPoint:locP]; //5.把path添加到数组中 [self.paths addObject:path]; }//手指移动- (void)touchesMoved:(NSSet
*)touches withEvent:(UIEvent *)event{ //1.获取触摸对象 UITouch * touch = touches.anyObject; //2.获取触摸点 CGPoint locP = [touch locationInView:touch.view]; //3.添加子路径 [[self.paths lastObject] addLineToPoint:locP]; //执行重绘 [self setNeedsDisplay]; }//手指抬起-(void)touchesEnded:(NSSet
*)touches withEvent:(UIEvent *)event{}- (void)drawRect:(CGRect)rect { // Drawing code //绘图 for (CZBezierPath * path in self.paths) { //设置颜色 [path.lineColor set]; //设置线头样式 path.lineCapStyle = kCGLineCapRound; //设置连接处的样式 path.lineJoinStyle = kCGLineJoinRound; [path stroke]; }}@end

转载地址:http://dhlgi.baihongyu.com/

你可能感兴趣的文章
图片的左右切换
查看>>
进级的RecyclerView——LRecyclerView
查看>>
Android 利用Gradle实现app的环境分离
查看>>
Android系统篇之----Binder机制和远程服务调用
查看>>
JavaScript DOM 属性
查看>>
Gradle 实现 Android 多渠道定制化打包
查看>>
Android开源项目及库整理总结
查看>>
Android快速开发系列 10个常用工具类
查看>>
深入理解JSON对象
查看>>
类似新浪微博帖子显示话题、@好友、表情解析等
查看>>
JSP 自动刷新
查看>>
Java应用架构的演化之路
查看>>
看透内存中的数组
查看>>
Android工程打包成jar文件,并且将工程中引用的jar一起打入新的jar文件中
查看>>
JS单例模式在工作中的使用
查看>>
Java易混小知识——equals方法和==的区别
查看>>
内置对象(Session、Application、ViewState)
查看>>
为什么Java有GC还需要自己来关闭某些资源?
查看>>
Android 热修复,插件式开发---基本知识
查看>>
JSP九大内置对象、四种作用域、跳转方式
查看>>