自定义UIView实现在屏幕上画画并记录路径

来源:互联网 发布:5g云网络wlxiu 编辑:程序博客网 时间:2024/04/30 15:07

#import "paint.h"

@implementation paint

- (void)awakeFromNib

{

这个方法只会执行一次

   self.path = [[UIBezierPathalloc] init];

    self.path.lineJoinStyle = kCGLineJoinRound;

    self.path.lineCapStyle = kCGLineCapRound;

   self.path.lineWidth =5;

初始化一个记录路径的数组

    _pathArray = [[NSMutableArrayalloc]init];

}


//必须实现的两个绘图方法

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

{

    //get the starting point

   CGPoint point = [[touches anyObject] locationInView:self];

    //move the path drawing cursor to the starting point

    [self.pathmoveToPoint:point];

}

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

{

    //get the current point

   CGPoint point = [[touches anyObject] locationInView:self];

    //add a new line segment to our path

    [self.pathaddLineToPoint:point];

    //redraw the view

    [selfsetNeedsDisplay];

}


//绘制方法

- (void)drawRect:(CGRect)rect

{

    //draw path

    [[UIColorclearColor] setFill];

    [[UIColorredColor] setStroke];

    [self.pathstroke];

    //添加路径

    [_pathArray addObject:self.path];

}

set和get方法用来传pathArray的值

- (NSArray *)pathArray

{

    return_pathArray;

}

- (void)setPathArray:(NSMutableArray *)pathArray

{

   _pathArray = pathArray;

}

暴漏一个draw方法,实现在其他类中调用自动绘图方法

- (void)draw:(UIBezierPath *)apath

{

   self.path = apath;

    [selfsetNeedsDisplay];

}



@end

0 0