#import "paint.h"
@implementation paint
- (void)awakeFromNib
{
这个方法只会执行一次
self.path = [[UIBezierPath alloc] init];
self.path.lineJoinStyle = kCGLineJoinRound;
self.path.lineCapStyle = kCGLineCapRound;
self.path.lineWidth = 5;
初始化一个记录路径的数组
_pathArray = [[NSMutableArray alloc]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.path moveToPoint: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.path addLineToPoint:point];
//redraw the view
[self setNeedsDisplay];
}
//绘制方法
- (void)drawRect:(CGRect)rect
{
//draw path
[[UIColor clearColor] setFill];
[[UIColor redColor] setStroke];
[self.path stroke];
//添加路径
[_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;
[self setNeedsDisplay];
}
@end
本文介绍了一个iOS绘图应用的具体实现细节,包括触摸开始和移动时的处理方法、路径的绘制方式以及路径数组的管理和更新机制。
3419

被折叠的 条评论
为什么被折叠?



