如何消除在自定义的UIView在iPhone上用手指画(How to erase finger pa

2019-06-25 15:15发布

我已经创建了一个手指涂料应用中的自定义的UIView(没有的.xib)。

油漆工作的罚款与自定义的UIView,但我的问题是,当我试图抹去画路径我得到:

错误:无效的情况下

下面是我的类:

.h文件中

@interface draw2D : UIView
{
    CGPoint previousPoint;
    CGPoint lastPoint;
    CGMutablePathRef path;
    UIButton *btnClose;
    UIButton *btnErase;
    BOOL IsErase;
}
- (IBAction)btnClose:(id)sender;
- (IBAction)btnErase:(id)sender;
@end


@implementation draw2D
- (void)awakeFromNib
{
    path = CGPathCreateMutable();
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        btnClose = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btnClose addTarget:self action:@selector(btnClose:)
         forControlEvents:UIControlEventTouchDown];
        [btnClose setTitle:@"close" forState:UIControlStateNormal];
        btnClose.frame = CGRectMake(10, 10, 100, 40.0);

        btnErase = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btnErase addTarget:self action:@selector(btnErase:)
           forControlEvents:UIControlEventTouchDown];
        [btnErase setTitle:@"Erase" forState:UIControlStateNormal];
        btnErase.frame = CGRectMake(150, 10, 100, 40.0);

        [self addSubview:btnClose];
        [self addSubview:btnErase];
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{    
    UITouch *touch = [touches anyObject];
    NSLog(@"Touch Began :%d",[touch tapCount]);

    if ([touch tapCount] > 1) 
    {
        NSLog(@"::::: Paint Start :::::");
        path = CGPathCreateMutable();
        previousPoint = lastPoint;
        [self setNeedsDisplay];
    }
    self.backgroundColor = [UIColor clearColor];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    NSLog(@"::::: touchesMoved :::::");
    lastPoint = [[touches anyObject] locationInView:self];
    previousPoint = [[touches anyObject] previousLocationInView:self];

    if(IsErase)
    {
        NSLog(@"erase");
        UITouch *erasetouch = [touches anyObject];  
        CGPoint erasecurrentPoint = [erasetouch locationInView:self];

        CGContextRef erasecontext = UIGraphicsGetCurrentContext();
        CGContextSetLineCap(erasecontext, kCGLineCapRound);
        CGContextSetLineWidth(erasecontext,10);
        CGContextSetBlendMode(erasecontext, kCGBlendModeClear);
        CGContextSetStrokeColorWithColor(erasecontext, [[UIColor clearColor] CGColor]);
        CGContextBeginPath(erasecontext);
        CGContextMoveToPoint(erasecontext, lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(erasecontext, erasecurrentPoint.x, erasecurrentPoint.y);
        CGContextStrokePath(erasecontext);     
        CGContextFlush(erasecontext);

    }  

    [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
    NSLog(@"::::: drawRect :::::");

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGPathMoveToPoint(path, NULL, previousPoint.x, previousPoint.y);
    CGPathAddLineToPoint(path, NULL, lastPoint.x, lastPoint.y);
    CGContextAddPath(context, path);
    CGContextSetLineWidth(context, 5);
    [[UIColor blueColor] setStroke];
    CGContextDrawPath(context, kCGPathFillStroke);
}

- (IBAction)btnClose:(id)sender
{
    [self removeFromSuperview];
}
- (IBAction)btnErase:(id)sender
{
    IsErase = YES;
}    

@end

我已删除按钮与功能,但不工作。

Answer 1:

你的问题是,你是不是应该叫UIGraphicGetContext()以外drawRect:

在你touchesBegan:withEvent:touchesMoved:withEvent: ,你应该简单地存储要绘制并调用点[self setNeedsDisplay]因为你现在做的事情。 在您drawRect:实现你再画已存储的点。

你可以看看这个GitHub库,它提供了一个平滑的描画的实现: https://github.com/levinunnink/Smooth-Line-View



Answer 2:

终于我有找到解决方案。 我的错误是在即时通讯实现擦除码touchMove方法与新context 。 我没有到需要新context 。 实现擦除代码drawrect方法,现在它工作正常。 看看下面的代码。

- (void)drawRect:(CGRect)rect
{

    [curImage drawAtPoint:CGPointMake(0, 0)];
    CGPoint mid1 = midPoint(previousPoint1, previousPoint2); 
    CGPoint mid2 = midPoint(currentPoint, previousPoint1);

    context = UIGraphicsGetCurrentContext(); 
    [self.layer renderInContext:context];

    if(IsErase)
    {
        CGContextSetLineWidth(context,self.lineWidth);
        CGContextSetBlendMode(context, kCGBlendModeClear);
        CGContextSetStrokeColorWithColor(context, [[UIColor clearColor] CGColor]);
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, mid1.x, mid1.y);
        CGContextAddLineToPoint(context, previousPoint1.x, previousPoint1.y);
        CGContextStrokePath(context);     
        CGContextFlush(context);
    }
    else
    {
        CGContextMoveToPoint(context, mid1.x, mid1.y);
        CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y); 
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetLineWidth(context, self.lineWidth);
        CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
        CGContextSaveGState(context);
        CGContextStrokePath(context);
    }
    [super drawRect:rect];
    [curImage release];
}

我希望这将有助于有人在擦除功能。



文章来源: How to erase finger paint on Custom UIView in iPhone