24H免费课程咨询  TEL:13401595960   QQ:1870218756  微信:13401595960(李老师)

东方博宜

网站首页 > 软件开发资讯 > IOS开发

【常州ios开发培训班】ios开发实现画板功能

2018-05-13 22:06:12 东方博宜 阅读

1、新建DWStroke类存储CGContextRef信息

DWStroke.h

#import <UIKit/UIKit.h>


typedef struct CGPath *CGMutablePathRef;

typedef enum CGBlendMode CGBlendMode;


@interface DWStroke : NSObject


@property (nonatomic) CGMutablePathRef path;

@property (nonatomic, assign) CGBlendMode blendMode;

@property (nonatomic, assign) CGFloat strokeWidth;

@property (nonatomic, strong) UIColor *lineColor;

- (void)strokeWithContext:(CGContextRef)context;

@end

DWStroke.m

- (void)strokeWithContext:(CGContextRef)context {

    CGContextSetStrokeColorWithColor(context, [_lineColor CGColor]);

    CGContextSetLineWidth(context, _strokeWidth);

    CGContextSetBlendMode(context, _blendMode);

    CGContextBeginPath(context);

    CGContextAddPath(context, _path);

    CGContextStrokePath(context);

}

2、画板

DrawTouchPointView.h

#import <UIKit/UIKit.h>

#import <QuartzCore/QuartzCore.h>


@interface DrawTouchPointView : UIView

/** 清屏 */

- (void)clearScreen;

/** 撤消操作 */

- (void)revokeScreen;

/** 擦除 */

- (void)eraseSreen;

/** 设置画笔颜色 */

- (void)setStrokeColor:(UIColor *)lineColor;

/** 设置画笔大小 */

- (void)setStrokeWidth:(CGFloat)lineWidth;

@end

DrawTouchPointView.m

@interface DrawTouchPointView () {

     CGMutablePathRef currentPath;//路径

}

//是否擦除

@property (nonatomic, assign) BOOL isEarse;

//存储所有的路径

@property (nonatomic, strong) NSMutableArray *stroks;

//画笔颜色

@property (nonatomic, strong) UIColor *lineColor;

//线条宽度

@property (nonatomic, assign) CGFloat lineWidth;

@end

初始化 ,背景颜色必须为[UIColor clearColor]

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        _stroks = [[NSMutableArray alloc] initWithCapacity:1];

        self.backgroundColor = [UIColor clearColor];

    }

    return self;

}

当用户点击画板存储路径的基本信息,并画起始点,根据使用擦除来设置blendMode属性和画笔的宽度,如果是擦除则blendMode = kCGBlendModeDestinationIn,画笔的大小为20,画笔颜色为[UIColor clearColor]


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    currentPath = CGPathCreateMutable();

    DWStroke *stroke = [[DWStroke alloc] init];

    stroke.path = currentPath;

    stroke.blendMode = _isEarse ? kCGBlendModeDestinationIn : kCGBlendModeNormal;

    stroke.strokeWidth = _isEarse ? 20.0 : _lineWidth;

    stroke.lineColor = _isEarse ? [UIColor clearColor] : _lineColor;

    [_stroks addObject:stroke];

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:self];

//    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 0);

//    CGPathAddArc(currentPath, &transform, point.x, point.y, _lineWidth/2.0, 0, 2*M_PI, 1);

    CGPathMoveToPoint(currentPath, NULL, point.x, point.y);

//    [self setNeedsDisplay];

}

开始移动, setNeedsDisplay 系统自动调用- (void)drawRect:(CGRect)rect

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:self];

    CGPathAddLineToPoint(currentPath, NULL, point.x, point.y);

    [self setNeedsDisplay];

}

在- (void)drawRect:(CGRect)rect中画图

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    for (DWStroke *stroke in _stroks) {

        [stroke strokeWithContext:context];

    }

}

清屏功能主要是移除数组中所有的路径并调用[self setNeedsDisplay]、设置_isEarse = NO

撤消功能主要是移除数组中最后一个对象并调用[self setNeedsDisplay]、设置_isEarse = NO

擦除功能只需修改isEarse属性为YES

画笔功能修改lineColor属性并设置_isEarse = NO

画笔大小功能修改strokeWidth属性并设置_isEarse = NO

/** 清屏 */

- (void)clearScreen {

    _isEarse = NO;

    [_stroks removeAllObjects];

    [self setNeedsDisplay];

}


/** 撤消操作 */

- (void)revokeScreen {

    _isEarse = NO;

    [_stroks removeLastObject];

    [self setNeedsDisplay];

}


/** 擦除 */

- (void)eraseSreen {

    self.isEarse = YES;

}

/** 设置画笔颜色 */

- (void)setStrokeColor:(UIColor *)lineColor {

    _isEarse = NO;

    self.lineColor = lineColor;

//    [self setNeedsDisplay];

}

/** 设置画笔大小 */

- (void)setStrokeWidth:(CGFloat)lineWidth {

    _isEarse = NO;

    self.lineWidth = lineWidth;

}

- (void)dealloc {

    CGPathRelease(currentPath);

}

常州东方博宜是一家专注IT,互联网,电脑方面的顶尖培训机构,欢迎社会及院校有志互联网营销,搜索引擎营销的人士,来东方博宜报名学习,互相交流。

常州东方博宜地址:常州天宁区延陵西路2号工人文化宫5号楼

 2.新北区太湖东路9-4号常州创意园E12


Powered by 东方博宜教育咨询江苏有限公司  ©2008-2018 www.czos.cn