UIView - 绘制多边形

// 需要多边形的视图
    UIView * delegation = [[UIView alloc]init];
    delegation.backgroundColor = HEXCOLOR(0x4d4d4d);
    [self.contentView addSubview:delegation];
    [delegation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.top.equalTo(self.grouponTypeLabel.mas_bottom).offset(YSCCommonPadding * 3);
        make.height.mas_equalTo(60);
    }];
    // 线的路径,坐标都是相对于所在视图
    UIBezierPath *polygonPath = [UIBezierPath bezierPath];
    // 起点
    [polygonPath moveToPoint:CGPointMake(SCREEN_WIDTH / 2 - 10, 0)];
    // 其他点
    [polygonPath addLineToPoint:CGPointMake(SCREEN_WIDTH / 2, - 10)];
    [polygonPath addLineToPoint:CGPointMake(SCREEN_WIDTH / 2 + 10, 0)];
    // 添加一个结尾点和起点相同
    [polygonPath closePath];
    
    CAShapeLayer *polygonLayer = [CAShapeLayer layer];
    polygonLayer.path = polygonPath.CGPath;
    polygonLayer.fillColor = HEXCOLOR(0x4d4d4d).CGColor; // 默认为blackColor
    [delegation.layer addSublayer:polygonLayer];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容