书法绘图IOS Coregraphics

我正在寻找一些标记为红色的标记,但不知道是否可以完成。

它看起来像使用书法笔标记。

在这里输入图像说明

按照“Andrey”的回答试用下面的代码,在绘图时我得到了下面的输出和空格。

在这里输入图像说明

刷图像可以在这里find使用的是 在这里输入图像说明

进一步修改代码,我发现仍然无法得到预期的结果。 在这里,我试图填补当前点和下一点之间存在的path。

- (void)drawRect:(CGRect)rect { // Drawing code CGFloat w=5.0; CGFloat h=2.0; CGContextRef context=UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); for(int i=0;i<self.positions.count;i++){ CGPoint point=[[self.positions objectAtIndex:i] CGPointValue]; CGFloat x=point.x-(w/2.0); CGFloat y=point.y-(h/2.0); CGContextFillRect(context, CGRectMake(x, y, w, h)); if(i+1<self.positions.count){ CGPoint nextPoint=[[self.positions objectAtIndex:(i+1)] CGPointValue]; CGMutablePathRef myPath=CGPathCreateMutable(); CGPathMoveToPoint(myPath, NULL, x, y); CGFloat x1=nextPoint.x-(w/2.0); CGFloat y1=nextPoint.y-(h/2.0); CGPathAddLineToPoint(myPath, NULL, x1, y1); CGPathAddLineToPoint(myPath, NULL, w, y1); CGPathAddLineToPoint(myPath, NULL, w, y); CGPathCloseSubpath(myPath); CGContextFillPath(context); } } } 

这些图片可以通过使用重写的drawRect:消息在UIView子类中绘制图像来完成。 您还需要添加某种触摸处理程序来捕捉触摸。 所以这里是一些代码:

 @interface DrawingView () @property (nonatomic, strong) NSMutableArray *positions; @end @implementation DrawingView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self awakeFromNib]; } return self; } - (void)awakeFromNib { self.positions = [[NSMutableArray alloc] init]; } - (void)drawRect:(CGRect)rect { [super drawRect:rect]; UIImage *brushImage = [UIImage imageNamed:@"brush.png"]; for (NSValue *object in self.positions) { CGPoint point = [object CGPointValue]; [brushImage drawAtPoint:CGPointMake(point.x - brushImage.size.width / 2.0, point.y - brushImage.size.height / 2.0)]; } } - (void)addPoint:(CGPoint)point { [self.positions addObject:[NSValue valueWithCGPoint:point]]; [self setNeedsDisplay]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; [self addPoint:[touch locationInView:self]]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; [self addPoint:[touch locationInView:self]]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; [self addPoint:[touch locationInView:self]]; } @end 

你只需要创build一个适当的brush.png ,并将这些视图放在任何你想要的地方。