书法绘画 IOS Coregraphics

4
我希望有一个标记物,用红色标注,但不确定是否可以实现。
看起来像是用毛笔笔尖标注。
试着按照“Andrey”的回答尝试以下代码后,我得到了以下带空格的绘图输出。
刷子图片可以在这里找到,使用的是enter image description here 对代码进行进一步更改后,我发现仍然无法获得预期的结果。 在这里,我正在尝试填充当前点和下一个点之间存在的路径。
- (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);
        }
    }
}

嗨,兄弟,你找到解决方案了吗? - Bhavesh.iosDev
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,并将它们放在任何您想要的位置。

从来没想过画它会这么容易。顺便说一下,编码时我发现有几个空格被遗漏了,这不应该发生,因为我们正在不断更新它在touchesMoved中。 - weber67
@weber67 touchesMoved 会跳过一些位置。你需要记住最后一个位置,并在中间插值出位置。 - Sulthan
@weber67,正如Sulthan上面提到的那样,你应该使用某种插值方法。正确的逻辑是使用从触摸中获取的点实现插值线绘制。但对于复杂的图形可能会比较慢。 - Andrei Chevozerov
你可以使用线条代替插值。只需创建一个带有图案的颜色,并在相邻点之间绘制线条即可。但是,需要在 touchesBegantouchesEnded 中添加一些额外的逻辑来让图形留出空白。如果需要,我可以更正我的答案 :) - Andrei Chevozerov
@AndreyChevozerov和Sulthan,谢谢。我已经编辑了我的代码,但是我发现我仍然得到相同的结果。请告诉我,我在哪里犯了错误。 - weber67
@weber67:我的想法是通过[UIColor colorWithPatternImage:brushImage];从图像中制作颜色,然后用这种颜色在点之间画线。 - Andrei Chevozerov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接