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

时间:2016-06-21 04:56:08

标签: ios objective-c

大家好,欢迎任何人解决我可以在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];


}

enter image description here

你可以在这里看到我的图像我可以绘制多条线但是如果我正在绘制新线,我需要绘制单线,现有线需要删除请任何人帮我解决这个问题。

提前致谢

1 个答案:

答案 0 :(得分:1)

像这样更改您的UIGestureRecognizer方法

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

希望这会对你有所帮助