
一:自定义view .h文件中代码如下
#import <UIKit/UIKit.h>@interface ZLpaintView : UIView @property(nonatomic, strong) UIColor *currentColor; - (void)back; - (void)clear; - (void)savetoFile:(NSString *)file;@end
.m中如下
#import "ZLpaintView.h"
@interface ZLpaintView()//用于存放 存放某条线的点 的数组
@property (nonatomic, strong) NSMutableArray *pointsOfAllLines;
//用于存放每条线的颜色
@property (nonatomic, strong) NSMutableArray *colorsOfAllLines;@end@implementation ZLpaintView
- (NSMutableArray *)pointsOfAllLines
{if (!_pointsOfAllLines) {_pointsOfAllLines = [NSMutableArray array];}return _pointsOfAllLines;
}
- (NSMutableArray *)colorsOfAllLines
{if (!_colorsOfAllLines) {_colorsOfAllLines = [NSMutableArray array];}return _colorsOfAllLines;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {// Drawing codeCGContextRef context = UIGraphicsGetCurrentContext();
// 设置线宽和收尾及连接点的样式CGContextSetLineWidth(context, 8);CGContextSetLineCap(context, kCGLineCapRound);CGContextSetLineJoin(context, kCGLineJoinRound);
// 遍历所有线NSInteger countOfLines = unt;// 取出每条线for (NSInteger i = 0; i < countOfLines; i ++) {NSArray *pointsOfline = self.pointsOfAllLines[i];NSInteger countOfPoints = unt;//设置这条线的颜色//取出对应线的颜色UIColor *currentColor = lorsOfAllLines[i];[currentColor set];//遍历这条线里的两个点for (NSInteger j = 0; j < countOfPoints; j ++) {CGPoint location = [pointsOfline[j] CGPointValue];if (j == 0) {CGContextMoveToPoint(context,location.x,location.y);}else{CGContextAddLineToPoint(context,location.x,location.y);}}CGContextStrokePath(context);//每画完一条线,渲染一次
}}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 一触摸屏幕,就建立一个数组用于存储一条线的点。NSMutableArray *pointsOfLine = [NSMutableArray array];
// 生成线后,将他的对应颜色也放进数组里if (!self.currentColor) {self.currentColor = [UIColor blackColor];//如果当前颜色为空,则设置黑色
[lorsOfAllLines addObject:self.currentColor];}else{[lorsOfAllLines addObject:self.currentColor];//否则直接加入到这个数组中
}
// 将生成的数组存放在自己属性数组中。
[self.pointsOfAllLines addObject:pointsOfLine];}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{UITouch *touch = [touches anyObject];CGPoint location = [touch locationInView:[touch view]];
// NSLog(@"%@",[NSValue valueWithCGPoint:location]);NSMutableArray * pointsOfLine = [self.pointsOfAllLines lastObject];[pointsOfLine addObject:[NSValue valueWithCGPoint:location]];NSLog(@"%@",pointsOfLine);[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{NSLog(@"%ld",unt);
}
- (void)back
{[self.pointsOfAllLines removeLastObject];[self setNeedsDisplay];
}
- (void)clear
{[self.pointsOfAllLines removeAllObjects];[self setNeedsDisplay];
}
- (void)savetoFile:(NSString *)file
{UIGraphicsBeginImageContext(self.bounds.size);[self.layer renderInContext:UIGraphicsGetCurrentContext()];UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();NSData *imageDate = UIImagePNGRepresentation(newImage);[imageDate writeToFile:file atomically:YES];
}
@end 二:在storyboard拖相应控件,并在控制器中实现相应地方法,代码如下:
#import "ViewController.h"
#import "ZLpaintView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet ZLpaintView *paintView;
- (IBAction)backClick;
- (IBAction)clearClick;
- (IBAction)saveClick;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}- (IBAction)backClick {[self.paintView back];
}- (IBAction)clearClick {[self.paintView clear];NSLog(@"%@",self.paintView.currentColor);
}- (IBAction)saveClick {[self.paintView savetoFile:@"/Users/mac/Desktop/TheImage.png"];
}
- (IBAction)colorBtnClick:(UIButton *)sender
{// 设置当前的颜色self.paintView.currentColor = sender.backgroundColor;
}
@end 三:效果:
转载于:.html
本文发布于:2024-02-02 03:57:20,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170681771041205.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
| 留言与评论(共有 0 条评论) |