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

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

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

效果截图:

Quartz2D复习(三) --- 涂鸦  涂鸦了 Quartz2D复习(三) --- 涂鸦  橡皮擦擦除 Quartz2D复习(三) --- 涂鸦 

保存到相册Quartz2D复习(三) --- 涂鸦

 

代码:

1、自定义PaintingBezierPath类继承自UIBezierPath类,增加一个自定义路径颜色的属性;自定义构造函数,设置颜色和路径宽度

PaintingBezierPath.h文件代码:

#import <UIKit/UIKit.h>

@interface PaintingBezierPath : UIBezierPath

@property (nonatomic, retain) UIColor *color; //线段的颜色

- (instancetype)initWithColor: (UIColor *)color WithWidth: (CGFloat)width WithStartPoint: (CGPoint)startPoint;

@end

PaintingBezierPath.m文件代码:

 1 //
 2 //  PaintingBezierPath.m
 3 //  tan_iosTwo
 4 //
 5 //  Created by xiaom on 15/7/22.
 6 //
 7 //  为了自定义每个轨迹的宽度和颜色,需要增加一个自定义方法
 8 
 9 #import "PaintingBezierPath.h"
10 
11 @implementation PaintingBezierPath
12 
13 - (instancetype)initWithColor:(UIColor *)color WithWidth:(CGFloat)width WithStartPoint:(CGPoint)startPoint{
14     if (self = [super init]){
15         self.color = color;
16         self.lineWidth = width;
17         self.lineJoinStyle = kCGLineJoinRound;
18         self.lineCapStyle = kCGLineCapRound;
19         [self moveToPoint:startPoint];
20     }
21     return self;
22 }
23 
24 @end
View Code

相关文章: