2015-10-09 76 views
0

我想單擊擦除按鈕,在移動線時擦除線。但我無法做到這一點。我成功實施了創建d線的代碼。但堅持如何抹掉Line。我在iOS中更新。這是我的代碼。請幫忙我們如何擦除線

@implementation Line 
{ 
    UIBezierPath *path; 
    UIImage *incrementalImage; // (1) 
} 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    if (self = [super initWithCoder:aDecoder]) 
    { 
     [self setMultipleTouchEnabled:NO]; 
     [self setBackgroundColor:[UIColor whiteColor]]; 
     path = [UIBezierPath bezierPath]; 
     [path setLineWidth:2.0]; 
    } 
    return self; 
} 
- (id)initWithFrame:(CGRect)frame 

{ 

    self = [super initWithFrame:frame]; 

    if (self) { 

     [self setMultipleTouchEnabled:NO]; 

     path = [UIBezierPath bezierPath]; 

       [path setLineWidth:1.0]; 
       self.backgroundColor=[UIColor clearColor]; 

     _button = [UIButton buttonWithType:UIButtonTypeCustom]; 
       [_button setTitle:@"" forState:UIControlStateNormal]; 
     _button.userInteractionEnabled=YES; 
       _button.frame =CGRectMake(100, 130, 100, 100); 
       _button.backgroundColor =[ UIColor redColor]; 
       [_button addTarget:self 
        action:@selector(erase) 
      forControlEvents:UIControlEventTouchUpInside]; 
     [self addSubview:_button]; 

    } 

    return self; 

} 

- (void)drawRect:(CGRect)rect 
{ 
    [incrementalImage drawInRect:rect]; // (3) 
    [path stroke]; 
} 


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:self]; 
    [path moveToPoint:p]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:self]; 
    [path addLineToPoint:p]; 
    [self setNeedsDisplay]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event // (2) 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:self]; 
    [path addLineToPoint:p]; 
    [self drawBitmap]; // (3) 
    [self setNeedsDisplay]; 
    [path removeAllPoints]; //(4) 
} 

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

- (void)drawBitmap // (3) 
{ 
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0); 
    [[UIColor blackColor] setStroke]; 
    if (!incrementalImage) // first draw; paint background white by ... 
    { 
     UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object 
     [[UIColor whiteColor] setFill]; 
     [rectpath fill]; // filling it with white 
    } 
    [incrementalImage drawAtPoint:CGPointZero]; 
    [path stroke]; 
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
} 

-(void)erase 
{ 
    NSLog(@"erasingLIne"); 
    } 

回答

1

BOOL值爲擦除。 BOOL isErase;

當擦除按鈕抽頭

if button tapped 
    { 
    isErase =Yes; 
    }else 
    { 
    isErase = NO; 
    } 

[路徑strokeWithBlendMode:isErase kCGBlendModeClear:kCGBlendModeNormal 阿爾法:1.0F];

希望它適合你。

+0

它刪除所有行,但我的問題擦除。 –