#import <UIKit/UIKit.h>
#import "DrawPath.h"
@interface QHView : UIView
-(void)clearView;
-(void)backView;
@property(nonatomic,strong)DrawPath *pathColor;
@property (strong,nonatomic) UIColor* drawColor;
@property (assign) CGFloat drawWidth;
@end
#import "QHView.h"
@interface QHView()
@property(nonatomic,strong)NSMutableArray *paths;
@end
@implementation QHView
//懒加载
-(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];
#warning -初始值有问题 小bug-
//这里刚开始数值为0 这里需要判断一下给一个初始值
if(self.drawWidth == 0)
{
[path setLineWidth:5];
}
else{
[path setLineWidth:self.drawWidth];
}
//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 *currentPath = [self.paths lastObject];
//4.设置当前路径的终点
[currentPath addLineToPoint:movePoint];
//5.调用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 *currentPath = [self.paths lastObject];
// //4.设置当前路径的终点
[currentPath addLineToPoint:endPoint];
// //5.调用drawRect 方法重回视图
[self setNeedsDisplay];
}
//画线
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
//[[UIColor redColor]set];
[self.drawColor set];//这里调用set 方法
//[self.pathColor.color set];
//边路数组绘制所有的线段
for (UIBezierPath *path in self.paths) {
[path stroke];
}
}
-(void)clearView
{
[self.paths removeAllObjects];
[self setNeedsDisplay];
}
-(void)backView{
[self.paths removeLastObject];
[self setNeedsDisplay];
}
@end
#import "QHViewController.h"
#import "QuartzCore/QuartzCore.h"
#import "QHView.h"
@interface QHViewController ()
@property (weak, nonatomic) IBOutlet QHView *customView;
@end
@implementation QHViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
//清屏
- (IBAction)clearBtn:(id)sender {
[self.customView clearView];
}
//回退
- (IBAction)backBtn:(id)sender {
[self.customView backView];
}
- (IBAction)saveBtn:(id)sender {
/**
保存截屏图片
*/
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"Save picture" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[self screenShot];
}
/**
* 改变线条的颜色
*
* @param sender <#sender description#>
*/
- (IBAction)redBtn:(id)sender {
UIButton *btn = (UIButton *)sender;
self.customView.pathColor.color = btn.backgroundColor;
NSLog(@"%@",btn.backgroundColor);
NSLog(@"%@=====",self.customView.pathColor.color);
}
- (IBAction)greenBtn:(id)sender {
UIButton *btn = (UIButton *)sender;
self.customView.drawColor = btn.backgroundColor;
}
- (IBAction)blueBtn:(id)sender {
UIButton *btn = (UIButton *)sender;
self.customView.drawColor = btn.backgroundColor;
}
- (IBAction)orangeBtn:(id)sender {
UIButton *btn = (UIButton *)sender;
self.customView.pathColor.color = btn.backgroundColor;
}
- (IBAction)yellowBtn:(id)sender {
UIButton *btn = (UIButton *)sender;
self.customView.pathColor.color = btn.backgroundColor;
}
- (IBAction)purBtn:(id)sender {
UIButton *btn = (UIButton *)sender;
self.customView.pathColor.color = btn.backgroundColor;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//调用系统原生的函数进行保存截屏图片
-(void) screenShot
{
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"image:%@",image);
UIImageView *imaView = [[UIImageView alloc] initWithImage:image];
imaView.frame = CGRectMake(0, 700, 500, 500);
[self.view addSubview:imaView];
//[imaView release];
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
}
//改变线条的宽度
- (IBAction)drawWidth:(id)sender {
UISlider* slider = (UISlider*)sender;
self.customView.drawWidth = slider.value;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end