一. 简单做一个画板
1. 建立一个UIView类
2. 在.m里建立一个延展
3. 分别定义一个起点, 一个终点的结构体属性 . 在建立一个存储路径的数组
@interface DrawView () { CGPoint _startPoint; CGPoint _endPoint; } @property (nonatomic, strong) NSMutableArray *pathArray; @end
4. 懒加载数组
- (NSMutableArray *)pathArray { if (_pathArray == nil) { _pathArray = [NSMutableArray array]; } return _pathArray; }
5. 开始绘制
(1) 起点
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // 获取起点 _startPoint = [[touches anyObject] locationInView:self]; // 创建贝瑟尔路径 UIBezierPath *path = [UIBezierPath bezierPath]; // 起点 [path moveToPoint:_startPoint]; // 添加到数组中 [self.pathArray addObject:path]; }
(2) 终点
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { _endPoint = [[touches anyObject] locationInView:self]; // 终点 UIBezierPath *path = [self.pathArray lastObject]; [path addLineToPoint:_endPoint]; [self setNeedsDisplay]; }
6. 重写 - (void)drawRect:(CGRect)rect 方法
- (void)drawRect:(CGRect)rect { // Drawing code for (UIBezierPath *path in self.pathArray) { // 设置颜色 [[UIColor orangeColor] set]; // 设置粗度 path.lineWidth = 10; // 渲染 [path stroke]; } }
二. 绘制饼状图
注 : 为了好看, 我随即分成了大小颜色都不同的99份
代码实现
#import "DrawView.h" #define kcolor arc4random()% 256 / 255.0 @implementation DrawView // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code NSMutableArray *array = [NSMutableArray array]; int sum = 0; for (int i = 1; i< 100; i ++) { int num = arc4random()% 100 ; sum += num; NSNumber *number = [NSNumber numberWithInt:num]; [array addObject:number]; } // 记录起点和终点 CGFloat start = 0; CGFloat end = 0; // 遍历数据 for (NSNumber *num in array) { end = num.floatValue / sum * M_PI * 2; UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:start endAngle:start + end clockwise:YES]; // 关闭路径 [path addLineToPoint:CGPointMake(150, 150)]; [[UIColor colorWithRed:kcolor green:kcolor blue:kcolor alpha:1] set]; [path closePath]; [path fill]; start += end; }
@end
三. 截图用法
1. 首先要有一个imageView, 所以在ViewContorller先建立一个UIImageView的属性 将背景的图片设置出来
2. 建立一个继承自UIView的类
3. 在这个UIView类.m里实现截图 (.h中写了一个block块用于传递路径)
.h 代码实现
// 传递路径 typedef void(^block)(UIBezierPath *p); @interface MyView : UIView @property (nonatomic, copy) block myblock; @end
.m 代码实现
#import "MyView.h" @interface MyView () // 保存路径 @property (nonatomic, strong) NSMutableArray *pathArray; @end @implementation MyView - (NSMutableArray *)pathArray { if (! _pathArray) { _pathArray = [NSMutableArray array]; } return _pathArray; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // 1. 获取起始点 并且创建路径 UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:[[touches anyObject] locationInView:self]]; // 2. 装入数组 [self.pathArray addObject:path]; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // 1. 获取终点 (不止一个) UIBezierPath *path = [self.pathArray lastObject]; [path addLineToPoint:[[touches anyObject] locationInView:self]]; // 2. 调用drawrect [self setNeedsDisplay]; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // 封闭路径 UIBezierPath *path = [self.pathArray lastObject]; [path closePath]; [self setNeedsDisplay]; // 调用block路径 self.myblock(path); } // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code for (UIBezierPath *path in self.pathArray) { path.lineWidth = 2; [[UIColor orangeColor] set]; [path stroke]; } } @end
4. 在ViewController中中回调路径
#import "ViewController.h" #import "MyImageView.h" #import "MyView.h" @interface ViewController () @property (nonatomic, strong) UIImageView *imageView; @property (nonatomic, strong) MyView *myView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 获取图片 // __block全局区 __block UIImage *image = [UIImage imageNamed:@"0"]; self.imageView = [[UIImageView alloc] initWithImage:image]; _imageView.frame = CGRectMake(7, 160, image.size.width, image.size.height); [self.view addSubview: _imageView]; self.myView = [[MyView alloc] initWithFrame:self.imageView.frame]; _myView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0]; [self.view addSubview:_myView]; // 剪切 __weak typeof(self) weakSelf = self; _myView.myblock = ^(UIBezierPath *p){ // 获取上下文 UIGraphicsBeginImageContextWithOptions(image.size, NO, 0); // 路径 UIBezierPath *path = p; // 剪切 [path addClip]; // 绘制 [image drawAtPoint:CGPointZero]; // 获取剪切后的图片 image = UIGraphicsGetImageFromCurrentImageContext(); // 结束上下文 UIGraphicsEndImageContext(); // 给imageView赋值 weakSelf.imageView.image = image; }; } @end
时间: 2024-11-20 20:32:11