fathima fathima - 5 months ago 33
iOS Question

how do i remove the line if draw the new line

hi everyone please anyone to solve the issues i can draw the line on

imageView
but i need to remove the existing line if draw the new line.....

if(selectBtnTag.tag==111)
{
[self selectedButtonBg:selectBtnTag];
// [self selectBtncolor_Action:@"111" :self.btn_wagonwheel :0];
//[self.img_pichmap setImage:[UIImage imageNamed:@"WagonWheel_img"]];
_View_Appeal.hidden=YES;
_view_Wagon_wheel.hidden=NO;



_View_Appeal.hidden=YES;
self.view_bowlType.hidden = YES;
self.view_fastBowl.hidden = YES;
self.view_aggressiveShot.hidden = YES;
self.view_defensive.hidden = YES;
self.img_pichmap.hidden=YES;
self.PichMapTittle.hidden=YES;

if(IS_IPAD_PRO)
{
self.height.constant=450;
self.width.constant=450;
}
else{
self.height.constant=350;
self.width.constant=350;
}

if([self.BatmenStyle isEqualToString:@"MSC012"])
{
[self.img_WagonWheel setImage:[UIImage imageNamed:@"LHWagon"]];
}
else{
[self.img_WagonWheel setImage:[UIImage imageNamed:@"RHWagon"]];
}

if (IS_IPAD_PRO) {
self.centerlbl=[[UILabel alloc]initWithFrame:CGRectMake(self.img_WagonWheel.frame.size.width/2+46, self.img_WagonWheel.frame.size.width/2+11, 5, 5)];
}
else

self.centerlbl=[[UILabel alloc]initWithFrame:CGRectMake(self.img_WagonWheel.frame.size.width/2-3, self.img_WagonWheel.frame.size.width/2-30, 5, 5)];

[self.centerlbl setBackgroundColor:[UIColor clearColor]];
[self.img_WagonWheel addSubview:self.centerlbl];

UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickWagonWheelmapTapAction:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired=1;
tapRecognizer.delegate=self;
[self.view_DrawlineWagon addGestureRecognizer:tapRecognizer];
[self.view_DrawlineWagon setUserInteractionEnabled:YES];
self.view_bowlType.hidden = YES;
self.view_fastBowl.hidden = YES;
self.view_aggressiveShot.hidden = YES;
self.view_defensive.hidden = YES;


}
}




-(void)didClickWagonWheelmapTapAction:(UIGestureRecognizer *)wagon_Wheelgesture
{
CGPoint p = [wagon_Wheelgesture locationInView:self.img_WagonWheel];
float Xposition = p.x;
float Yposition = p.y;
CGMutablePathRef straightLinePath = CGPathCreateMutable();
CGPathMoveToPoint(straightLinePath, NULL, Xposition, Yposition);
CGPathAddLineToPoint(straightLinePath, NULL,self.centerlbl.center.x,self.centerlbl.center.y);

// CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),self.backgroundColor.CGColor);
//
// CGContextFillRect(UIGraphicsGetCurrentContext(), rect);


CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = straightLinePath;
UIColor *fillColor = [UIColor redColor];
shapeLayer.fillColor = fillColor.CGColor;
UIColor *strokeColor = [UIColor redColor];
shapeLayer.strokeColor = strokeColor.CGColor;
shapeLayer.lineWidth = 2.0f;
shapeLayer.fillRule = kCAFillRuleNonZero;


[self.img_WagonWheel.layer addSublayer:shapeLayer];


}


enter image description here

You can see my images here i can draw the multiple line but i need to draw the single line if i am drawing the new line the existing line need to remove please any one help me to solve this issues.Thanks in advance

Answer

Change your UIGestureRecognizer method like this

-(void)didClickWagonWheelmapTapAction:(UIGestureRecognizer *)wagon_Wheelgesture {

    for (CALayer *layer in self.img_WagonWheel.layer.sublayers) {
        if ([layer.name isEqualToString:@"DrawLine"]) {
            [layer removeFromSuperlayer];
            break;
        }
    }
    CGPoint p = [recognizer locationInView:self.imgDraw];
    float Xposition = p.x;
    float Yposition = p.y;
    CGMutablePathRef straightLinePath = CGPathCreateMutable();
    CGPathMoveToPoint(straightLinePath, NULL, Xposition, Yposition);
    CGPathAddLineToPoint(straightLinePath, NULL,self.view.center.x,self.view.center.y);
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = straightLinePath;
    UIColor *fillColor = [UIColor redColor];
    shapeLayer.fillColor = fillColor.CGColor;
    UIColor *strokeColor = [UIColor redColor];
    shapeLayer.strokeColor = strokeColor.CGColor;
    shapeLayer.lineWidth = 2.0f;
    shapeLayer.fillRule = kCAFillRuleNonZero;
    shapeLayer.name = @"DrawLine";
    [self.imgDraw.layer addSublayer:shapeLayer];
}

Hope this will help you

Comments