橡皮擦工具矢量绘图ios

时间:2014-07-22 07:25:09

标签: ios objective-c core-graphics

我正在实施我的绘图工具:

CGPoint CGPointCropByRect1(CGPoint point, CGRect rect)
{
    rect = CGRectInset(rect, 5., 5.);
    return CGPointMake(MIN(CGRectGetMaxX(rect), MAX(CGRectGetMinX(rect), point.x)), MIN(CGRectGetMaxY(rect), MAX(CGRectGetMinY(rect), point.y)));
}

CGFloat CGPointLengthToPoint1(CGPoint first, CGPoint second)
{
    return sqrtf(powf(second.x - first.x, 2.) + powf(second.y - first.y, 2.));
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _paths = [[NSMutableArray alloc] init];
    }
    return self;

}


- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);

    UIColor *color = [UIColor blackColor];
    CGFloat width = 5.f;
    if (_isErase) {
        CGContextSetStrokeColorWithColor(ctx, [UIColor clearColor].CGColor);
        //CGContextSetFillColorWithColor(ctx, [UIColor clearColor].CGColor);

        CGContextSetBlendMode(ctx, kCGBlendModeClear);

    }

    else {

    CGContextSetStrokeColorWithColor(ctx, [color CGColor]);
    CGContextSetLineWidth(ctx, width);
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    CGContextSetLineCap(ctx, kCGLineCapRound);
    }
    for(NSArray *array in _paths)
    {
        size_t index = 0;
        CGPoint first = [[array objectAtIndex:index++] CGPointValue];

        CGContextBeginPath(ctx);
        CGContextMoveToPoint(ctx, first.x, first.y);
        while (index < array.count)
        {
            CGPoint middlePoint = [[array objectAtIndex:index++] CGPointValue];
            if(index < array.count)
            {
                CGPoint endPoint = [[array objectAtIndex:index++] CGPointValue];
                //CGContextAddQuadCurveToPoint(ctx, middlePoint.x, middlePoint.y, endPoint.x, endPoint.y);
                CGContextAddLineToPoint(ctx, middlePoint.x, middlePoint.y);
                CGContextAddLineToPoint(ctx, endPoint.x, endPoint.y);
            }
            else
                CGContextAddLineToPoint(ctx, middlePoint.x, middlePoint.y);
        }
        CGContextStrokePath(ctx);
    }

    CGContextBeginPath(ctx);
    BOOL first = YES;
    for(NSValue *point in _currentPath)
    {
        if(first)
            CGContextMoveToPoint(ctx, point.CGPointValue.x, point.CGPointValue.y);
        else
            CGContextAddLineToPoint(ctx, point.CGPointValue.x, point.CGPointValue.y);
        first = NO;
    }
    CGContextStrokePath(ctx);

    CGContextRestoreGState(ctx);
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    _currentPath = [[NSMutableArray alloc] init];
    [_currentPath addObject:[NSValue valueWithCGPoint:[[touches anyObject] locationInView:self]]];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint currentTouch = [[touches anyObject] locationInView:self];

    if(!CGRectContainsPoint(self.bounds, currentTouch))
    {
        if(!_currentPath.count)
            return;

        currentTouch = CGPointCropByRect1(currentTouch, self.bounds);
        CGPoint lastTouch = [[_currentPath objectAtIndex:_currentPath.count - 1] CGPointValue];
        [_currentPath addObject:[NSValue valueWithCGPoint:currentTouch]];
        [self setNeedsDisplayInRect:CGRectInset(CGRectMake(currentTouch.x, currentTouch.y, lastTouch.x - currentTouch.x, lastTouch.y - currentTouch.y), -10., -10.)];
        return;
    }

    if(_currentPath.count == 0)
    {
        _currentPath = [[NSMutableArray alloc] init];
        [_currentPath addObject:[NSValue valueWithCGPoint:currentTouch]];
        return;
    }

    CGPoint lastTouch = [[_currentPath objectAtIndex:_currentPath.count - 1] CGPointValue];

    CGFloat dim = CGPointLengthToPoint1(currentTouch, lastTouch);
    if(dim > 5.)
    {
        [_currentPath addObject:[NSValue valueWithCGPoint:currentTouch]];
        [self setNeedsDisplayInRect:CGRectInset(CGRectMake(currentTouch.x, currentTouch.y, lastTouch.x - currentTouch.x, lastTouch.y - currentTouch.y), -10., -10.)];
    }

}
-(void)dropPath
{
    if(_currentPath.count)
        [_paths addObject:[NSArray arrayWithArray:_currentPath]];

    [_currentPath release];
    _currentPath = nil;
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint currentTouch = [[touches anyObject] locationInView:self];
    if (!_isErase) {
        //CGPoint currentTouch = [[touches anyObject] locationInView:self];
        [_currentPath addObject:[NSValue valueWithCGPoint:currentTouch]];
        [self dropPath];
        [self setNeedsDisplay];
    }
    else{

        [_currentPath removeObject:[NSValue valueWithCGPoint:currentTouch]];
        [self dropPath];
        [self setNeedsDisplay];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesEnded:touches withEvent:event];
}
@end

主要思想是 - 我将当前点存储在NSArray中,然后将其添加到另一个阵列中。 我该如何制作橡皮擦?我认为主要的想法是重绘线条,但如何实现呢?有没有一些例子可以让我清楚? 使用背景颜色的选项不合适。 感谢

1 个答案:

答案 0 :(得分:0)

您可以尝试添加此片段:

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextAddPath(context, self.path);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, [UIColor clear].CGColor);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextSetAlpha(context, self.lineAlpha);
CGContextStrokePath(context);