您的位置:首页 > 产品设计 > UI/UE

IOS 画图板-使用UIBezierPath

2016-05-24 00:57 751 查看


uiviewcontroller.m

//
// ViewController.m
// 06-画图
//
// Created by panba on 16-5-23.
// Copyright (c) 2016年 panba. All rights reserved.
//

#import "ViewController.h"
#import "JCView.h"
@interface ViewController ()
@property(strong,nonatomic) JCView *myview;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//1-添加view
JCView *myview = [[JCView alloc]initWithFrame:CGRectMake(0, 100, 320, 320)];
myview.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myview];
self.view.backgroundColor = [UIColor grayColor];
self.myview = myview;
//2-添加清屏,回退,保存按钮
UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
clearBtn.frame = CGRectMake(20, 60, 50, 44);
[clearBtn setTitle:@"清屏" forState:UIControlStateNormal];
[clearBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:clearBtn];
[clearBtn addTarget:self action:@selector(clearClick) forControlEvents:UIControlEventTouchUpInside];

UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
backBtn.frame = CGRectMake(100, 60, 50, 44);
[backBtn setTitle:@"回退" forState:UIControlStateNormal];
[backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:backBtn];
[backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];

UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
saveBtn.frame = CGRectMake(180, 60, 50, 44);
[saveBtn setTitle:@"保存" forState:UIControlStateNormal];
[saveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:saveBtn];
[saveBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
}
-(void)clearClick
{
[self.myview clearClick];
}
-(void)backClick
{
[self.myview backClick];
}
-(void)saveClick
{
//1-得到bit上下文
UIGraphicsBeginImageContext(self.myview.frame.size) ;
//2-绘图到上下文上
[self.myview.layer renderInContext:UIGraphicsGetCurrentContext()];
//3-得到新图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//4-保存
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
NSLog(@"保存失败");
}
else
{
NSLog(@"保存成功");
}

}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


JCView.h
//
// JCView.h
// 06-画图
//
// Created by panba on 16-5-23.
// Copyright (c) 2016年 panba. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface JCView : UIView
-(void)clearClick;
-(void)backClick;
-(void)saveClick;

@end


JCView.m
//
// JCView.m
// 06-画图
//
// Created by panba on 16-5-23.
// Copyright (c) 2016年 panba. All rights reserved.
//

#import "JCView.h"

@interface JCView ()
@property (nonatomic,strong) NSMutableArray *totalPoint;

@end

@implementation JCView

//懒加载
-(NSMutableArray *)totalPoint
{
if (_totalPoint == nil) {
_totalPoint = [NSMutableArray array];
}
return _totalPoint;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//1-得到开始点
CGPoint startPoint = [touch locationInView:touch.view];
// //2-创建小数组
// NSMutableArray *subtarr = [NSMutableArray array];
// [subtarr addObject:[NSValue valueWithCGPoint:startPoint]];
// //3-将小数组添加到大数组
// [self.totalPoint addObject:subtarr];
//2-创建路径
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startPoint];
//3-添加到数组
[self.totalPoint addObject:path];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//1-得到停止点
CGPoint movePoint = [touch locationInView:touch.view];
// //2-得到小数组
// NSMutableArray *subtarr = [self.totalPoint lastObject];
// [subtarr addObject:[NSValue valueWithCGPoint:movePoint]];
//
// //4-渲染
// [self setNeedsDisplay];
//2-得到路径
UIBezierPath *currpath = [self.totalPoint lastObject];
[currpath addLineToPoint:movePoint];
//4-渲染
[self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//1-得到当前点
CGPoint endPoint = [touch locationInView:touch.view];
// //2-得到小数组
// NSMutableArray *subarr = [self.totalPoint lastObject];
// [subarr addObject:[NSValue valueWithCGPoint:endPoint]];
//2-得到路径
UIBezierPath *currpath = [self.totalPoint lastObject];
[currpath addLineToPoint:endPoint];

//4-渲染
[self setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect
{
for (UIBezierPath *path in self.totalPoint) {
[path stroke];
}
}
-(void)test
{
// Drawing code
//1-获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();

//2-划线
for (NSMutableArray *subarr in self.totalPoint) {
for (int index = 0; index <subarr.count; index++) {
CGPoint point = [subarr[index] CGPointValue];
if (0 == index) {
CGContextMoveToPoint(ctx, point.x, point.y);
}
else
{
CGContextAddLineToPoint(ctx, point.x, point.y);
}
}
}
// CGContextMoveToPoint(ctx, <#CGFloat x#>, <#CGFloat y#>);
// CGContextAddLineToPoint(ctx, <#CGFloat x#>, <#CGFloat y#>);

//3-渲染
CGContextStrokePath(ctx);
}

-(void)clearClick
{
[self.totalPoint removeAllObjects];
[self setNeedsDisplay];
}
-(void)backClick
{
[self.totalPoint removeLastObject];
[self setNeedsDisplay];
}
-(void)saveClick
{

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 画图板