IOS开发之利用UIBezierPath动态绘制多边形,并手拽坐标点动态改变图形形状

前两天公司项目需求,需要绘制一个多边形并且能进行拖拽改装图形形状,在网上搜索了一圈关于UIBezierPath的用法
大家举的范例都很死,没有一个 动态绘制图形的

因此 跟大家分享动如做到动态绘制并且动态改变

使用UIBezierPath绘制分两种:

1.一般创建一个UIView子类, 重写其中的drawRect方法,再drawRect方法中利用UIBezierPath添加画图

2.利用CAShapeLayer的path属性来替换UIBezierPath.CGPath,然后将CAShapeLayer放到你需要的View上

 

定义一个数组,获取你在view上的点tap到的点

@property (nonatomic,strong)NSMutableArray *pointArray;

@property (nonatomic,strong)CAShapeLayer *shapelayer;

/*---------------获取你在界面上点击的点----------------*/

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

        if (event.type==UIEventTypeTouches) {

            UITouch *touch = [event.allTouches anyObject];

            if (touch.phase == UITouchPhaseBegan) {

                CGPoint point = [touch locationInView:self.view]; //返回触摸点在视图中的当前坐标

                int x = point.x;

                int y = point.y;

                  NSLog(@"touch (x, y) is (%d, %d)", x, y);

                  [_pointArray addObject:NSStringFromCGPoint(point)];

                   [self toDrawPictureNow];

            }

        }

}

- (void)toDrawPictureNow

{

          if (_pointArray.count > 1)

            {

                 for (NSInteger i = 0; i < _pointArray.count - 1; i ++) {

                      UIView *pointView = (UIView *)[self.view viewWithTag:100 + i];

                      [pointView removeFromSuperview];

                      pointView = nil;

                }

           } 

      if(_shapelayer)

      {

           [_shapelayer removeFromSuperlayer];

           _shapelayer = nil;

      }

//只有一个点

     if (_pointArray.count == 1) {

        CGPoint point= CGPointFromString([_pointArray objectAtIndex:0]);

        UIView *pointView = [[UIView alloc]init];

        pointView.frame = CGRectMake(0, 0, 20, 20);

        pointView.center = point;

        pointView.tag = 100;

        pointView.layer.cornerRadius = 10;

        pointView.backgroundColor = [UIColor redColor];

        [self.view addSubview:pointView];

    }else

    {

            UIBezierPath *path = [UIBezierPath bezierPath];

            for (NSInteger i = 0; i < _pointArray.count; i++) {

               CGPoint point= CGPointFromString([_pointArray objectAtIndex:i]);

                if (i == 0) {

                [path moveToPoint:point];

               }else

               {

                [path addLineToPoint:point];

               }

                  UIView *pointView = [[UIView alloc]init];

                  pointView.frame = CGRectMake(0, 0, 20, 20);

                  pointView.tag = 100 + i;

                  pointView.center = point;

                  pointView.layer.cornerRadius = 10;

                  pointView.backgroundColor = [UIColor redColor];

                  [self.view addSubview:pointView];

                   UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureAction:)];

                    [pointView addGestureRecognizer:pan];

          }

        [path stroke];

        [path fill];

        [path closePath];

         _shapelayer = [CAShapeLayer layer];

        //设置边框颜色

        _shapelayer.strokeColor = [[UIColor redColor]CGColor];

        //设置填充颜色 如果只要边 可以把里面设置成[UIColor ClearColor]

        _shapelayer.fillColor = [[UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:0.5]CGColor];

        //就是这句话在关联彼此(UIBezierPath和CAShapeLayer):

        _shapelayer.path = path.CGPath;

        [self.view.layer addSublayer:shapelayer];

    }

}

/*---------------------处理移动手势--------------*/

- (void)panGestureAction:(UIPanGestureRecognizer *)gesture

{

    CGPoint point = [gesture translationInView:gesture.view];

    [gesture setTranslation:CGPointZero inView:gesture.view];

    [self reloadViewWithPoint:point andItme:gesture.view.tag - 99];

}

- (void)reloadViewWithPoint:(CGPoint)itemPoint andItme:(NSInteger)itemp

{

    CGPoint orignialPoint = CGPointFromString([_pointArray objectAtIndex:itemp - 1]);

    CGPoint desPoint = CGPointMake(orignialPoint.x + itemPoint.x, orignialPoint.y + itemPoint.y);

    NSLog(@"Finaltouch(%f, %f)", desPoint.x, desPoint.y); 

    [_pointArray replaceObjectAtIndex:(itemp - 1) withObject:NSStringFromCGPoint(desPoint)];

    [self  toDrawPictureNow]  ; 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值