Ios 如果绘制新线,如何删除该线

Ios 如果绘制新线,如何删除该线,ios,objective-c,Ios,Objective C,大家好,请大家解决问题,我可以在UIImageView上画线,但如果画新线,我需要删除现有的线 if(selectBtnTag.tag==111) { [self selectedButtonBg:selectBtnTag]; // [self selectBtncolor_Action:@"111" :self.btn_wagonwheel :0]; //[self.img_pichmap setIm

大家好,请大家解决问题,我可以在
UIImageView
上画线,但如果画新线,我需要删除现有的线

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];


}

你可以在这里看到我的图片,我可以画多条线,但我需要画一条线如果我画的是新线,现有的线需要删除,请任何人帮助我解决这个问题


提前感谢

更改您的
UIgestureRecognitor
方法,如下所示

-(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];
}

希望这将帮助您

使用此[self.shapeLayer removeFromSuperlayer]删除现有行;并创建一个新的行你不是类型短信,所以避免使用u plz和所有的排序字k罚款,我会在这里后,你好,哈里,你能告诉我,我应该在哪里实施的code@fathima如果(wagon_wheelspirate.state==UIgestureRecognitizerStateStarted){[self.shapeLayer removeFromSuperlayer],则需要删除处于手势状态的形状层}在DID中,单击WagonWheelMapTap动作手势事件如果我画另一条线…现有的应该被删除,但如果我继续触摸现有的绘制线visible@fathima我已经编辑了答案,请检查。它在我这边工作-(void)didClickWagonWheelmapTapAction:(UIGestureRecognitor*)WagonWheel手势{for(self.img_WagonWheel.layer.sublayers中的CALayer*层){if([layer.name IsequalString:@“DrawLine”]){[layer removeFromSuperlayer];break;}}CGP=[货车姿态位置查看:self.img\u货车];float Xposition=p.x;float Yposition=p.y;CGMutablePathRef straightLinePath=CGPathCreateMutable();CGPathMoveToPoint(straightLinePath,NULL,Xposition,Yposition);CGPathAddLineToPoint(straightLinePath,NULL,self.centerbl.center.x,self.centerbl.center.y);CAShapeLayer*shapeLayer=[CAShapeLayer层];shapeLayer.path=直线路径;UIColor*fillColor=[UIColor redColor];shapeLayer.fillColor=fillColor.CGColor;UIColor*strokeColor=[UIColor redColor];shapeLayer.strokeColor=strokeColor.CGColor;shapeLayer.lineWidth=2.0f;shapeLayer.fillRule=kCAFillRuleNonZero;