- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 3); //线宽
CGContextSetAllowsAntialiasing(context, true);
CGContextSetRGBStrokeColor(context,70.0/255.0,241.0/255.0,241.0/255.0,1.0); //线的颜色
CGContextBeginPath(context);
CGContextMoveToPoint(context, 100, 100); //起点坐标
CGContextAddLineToPoint(context, 100, 300); //终点坐标
CGContextAddLineToPoint(context, 200, 300);
CGContextAddLineToPoint(context, 200, 160);
CGContextAddLineToPoint(context, 230, 145);
CGContextAddLineToPoint(context, 200, 130);
CGContextAddLineToPoint(context, 200, 100);
CGContextAddLineToPoint(context, 100, 100);
CGContextClosePath(context);
CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);//填充颜色
CGContextDrawPath(context,kCGPathEOFill);//根据坐标绘制路径
}
//也可以用这种方法
- (CAShapeLayer*)creatMaskLayerWithView:(UIView*)view
{
CGFloatviewWidth =CGRectGetWidth(view.frame);
CGFloatviewHeight =CGRectGetHeight(view.frame);
CGFloatrightSpace =10;
CGFloattopSpace =15;
CGPointpoint1 =CGPointMake(0,0);
CGPointpoint2 =CGPointMake(viewWidth - rightSpace,0);
CGPointpoint3 =CGPointMake(viewWidth - rightSpace, topSpace);
CGPointpoint4 =CGPointMake(viewWidth, topSpace);
CGPointpoint5 =CGPointMake(viewWidth - rightSpace, topSpace +10);
CGPointpoint6 =CGPointMake(viewWidth - rightSpace, viewHeight);
CGPointpoint7 =CGPointMake(0, viewHeight);
UIBezierPath * path = [UIBezierPath bezierPath];
[pathmoveToPoint:point1];
[pathaddLineToPoint:point2];
[pathaddLineToPoint:point3];
[pathaddLineToPoint:point4];
[pathaddLineToPoint:point5];
[pathaddLineToPoint:point6];
[pathaddLineToPoint:point7];
[pathclosePath];
CAShapeLayer * layer = [CAShapeLayer layer];
layer.path= path.CGPath;
returnlayer;
}
这样调用
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(40, 50, 80, 100)];
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
view.layer.mask = [self creatMaskLayerWithView:view];
网友评论