美文网首页
iOS绘制三角形

iOS绘制三角形

作者: 一笔一划_py | 来源:发表于2019-01-07 00:58 被阅读0次

    .m

    - (instancetype)initWithFrame:(CGRect)frame{
        if (self = [super initWithFrame:frame]) {
            self.backgroundColor = [UIColor cyanColor];
        }
        return self;
    }
    
    - (void)drawRect:(CGRect)rect {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, rect.size.width*0.5, 0);//设置起始位置
        CGContextAddLineToPoint(context,20, rect.size.height);//从起始位置到这个点连线
        CGContextAddLineToPoint(context,rect.size.width-20, rect.size.height);
        CGContextClosePath(context);//结束画线..自动封闭  不写也可封闭
        [[UIColor redColor] setFill]; //设置填充色 不设置默认黑色
        [[UIColor clearColor] setStroke];//边框颜色,不设置默认黑色
        CGContextDrawPath(context, kCGPathFillStroke);//绘制路径
    }
    

    效果图:

    Simulator Screen Shot - iPhone 6 - 2019-01-07 at 00.59.44.png

    相关文章

      网友评论

          本文标题:iOS绘制三角形

          本文链接:https://www.haomeiwen.com/subject/gdcyrqtx.html