您的位置:首页 > 其它

Quartz2D复习(三) --- 涂鸦

2015-07-24 00:44 267 查看
和上一篇手势解锁不一样,手势解锁只画了一条路径,从触摸开始--》触摸移动--》触摸结束 ,然后路径完成了,渲染出来就是手势解锁了;

这次涂鸦想做到的效果是可以画很多次线段或弧,每次又可以设置不同的宽度和颜色,然后还要有撤销、清屏、橡皮擦的功能,那就需要画很多条路径了,然后每条路径有自己的颜色和宽度,那么

UIBezierPath类也实现不了,需要自定义一个类,继承自UIBezierPath,然后再增加自己的颜色和宽度属性。

效果截图:

//  信手涂鸦

#import "DoodleViewController.h"
#import "PaintingView.h"

@interface DoodleViewController ()

@property (nonatomic, retain) PaintingView *paintV; //涂鸦的画板

@end

@implementation DoodleViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view setBackgroundColor:[UIColor whiteColor]];

//自定义View涂鸦
PaintingView *v = [[PaintingView alloc] initWithFrame:CGRectMake(0, 80, 320, 450)];
[v setBackgroundColor:[UIColor grayColor]];
[v setAlpha:0.6];
[self.view addSubview:v];
self.paintV = v;

[self addReturnBtn];    //添加返回按钮
[self addDoodleSetWidthAndColor]; //增加设置涂鸦的宽度和颜色设置
}

//添加返回按钮
- (void)addReturnBtn{
UIButton *returnBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 50, 30)];
[returnBtn setTitle:@"返回" forState:UIControlStateNormal];
[returnBtn addTarget:self action:@selector(returnPrePage) forControlEvents:UIControlEventTouchUpInside];
[returnBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.view addSubview:returnBtn];
}

//添加涂鸦宽度设置和颜色设置
- (void)addDoodleSetWidthAndColor{
//1、增加UISlider用来设置可调节宽度
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(50, 20, 80, 30)];
slider.maximumValue = 15.0f; //最大值
slider.value = 3.0f;    //默认为1
self.paintV.lineWidth = slider.value;
[slider addTarget:self action:@selector(setLineWidth:) forControlEvents:UIControlEventValueChanged]; //绑定值改变事件
[self.view addSubview:slider];

//2、添加颜色选择按钮
NSArray *colors = @[[UIColor redColor], [UIColor yellowColor], [UIColor greenColor], [UIColor blueColor], [UIColor purpleColor], [UIColor brownColor]];
for (int i = 0; i < colors.count; i++) {
CGFloat x =  140 + 30 * i;
UIButton *colorBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, 25, 20, 20)];
[colorBtn setBackgroundColor:[colors objectAtIndex:i]];
[colorBtn addTarget:self action:@selector(setLineColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:colorBtn];
}

//第二行
//3、添加撤销按钮
UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 55, 60, 25)];
[cancelBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[cancelBtn setTitle:@"撤销" forState:UIControlStateNormal];
[cancelBtn addTarget:self action:@selector(cancelPainting) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cancelBtn];

//4、清屏按钮
UIButton *clearScreenBtn = [[UIButton alloc] initWithFrame:CGRectMake(70, 55, 60, 25)];
[clearScreenBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[clearScreenBtn setTitle:@"清屏" forState:UIControlStateNormal];
[clearScreenBtn addTarget:self action:@selector(clearScreen) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clearScreenBtn];

//5、添加一个橡皮擦
UIButton *brushBtn = [[UIButton alloc] initWithFrame:CGRectMake(140, 55, 60, 25)];
[brushBtn setTitle:@"橡皮擦" forState:UIControlStateNormal];
[brushBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[brushBtn addTarget:self action:@selector(brush) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:brushBtn];

//6、保存到相册按钮
UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(210, 55, 100, 25)];
[saveBtn setTitle:@"保存到相册" forState:UIControlStateNormal];
[saveBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[saveBtn addTarget:self action:@selector(saveImgToAlbum) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:saveBtn];
}

//调节宽度
- (void)setLineWidth:(UISlider *)sender{
[self.paintV setLineWidth:sender.value];
}

//调节颜色
- (void)setLineColor: (UIButton *)sender{
[self.paintV setLineColor:sender.backgroundColor];
}

//设置橡皮擦
- (void)brush{
self.paintV.lineColor = self.paintV.backgroundColor;
if (self.paintV.lineWidth < 5) self.paintV.lineWidth = 5;
}
//撤销
- (void)cancelPainting{
[self.paintV cancelPainting];
}
//清屏
- (void)clearScreen{
[self.paintV clearScreen];
}

//保存图片到相册
- (void)saveImgToAlbum{
[self.paintV saveImgToAlbum];
}

//返回上一页
- (void)returnPrePage{
[self dismissViewControllerAnimated:YES completion:nil];
}

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

/*
#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


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