4

我正在寻找用红色标记的东西作为标记,但不知道是否可以完成。

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

在此处输入图像描述

尝试“Andrey”回答的以下代码,我在绘图时得到以下带有空格的输出。

在此处输入图像描述

刷图可以在这里找到使用的是在此处输入图像描述

对代码进行进一步更改,我发现我仍然无法获得预期的结果。在这里,我试图填充当前点和下一点之间存在的路径。

- (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);
        }
    }
}
4

1 回答 1

2

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

@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

您只需要创建一个适当brush.png的视图并将这些视图放置在您想要的任何位置。

于 2013-05-27T15:44:36.083 回答