前两天公司项目需求,需要绘制一个多边形并且能进行拖拽改装图形形状,在网上搜索了一圈关于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] ;
}