美文网首页
iOS 画三角形、圆形、矩形,一看就会那种

iOS 画三角形、圆形、矩形,一看就会那种

作者: 邓布利多教授 | 来源:发表于2019-07-24 16:27 被阅读0次

首先,创建一个View,在View的.m文件写上-(void)drawRect:(CGRect)rect;方法

1、三角形

#pragma mark - 画三角形
-(void)drawTrianglePath:(CGRect)rect {
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(rect.size.width, 0)];
    [path addLineToPoint:CGPointMake(rect.size.width / 2, rect.size.height)];
    
    /*
     闭合线可以通过下面的两种方式实现
     [path closePath];
     [path addLineToPoint:CGPointMake(20, 20)];
     */
    [path closePath];
    
    path.lineWidth = 1.5;//设置线宽
    
    UIColor *fillColor = [UIColor greenColor];
    [fillColor set];//使设置的颜色生效
    [path fill];//填充整个三角区颜色
    
    /*
     设置边线颜色
     */
    UIColor *strokeColor = fillColor;
    [strokeColor set];//使设置的颜色生效
    
    [path stroke];//连线
    
}

2、圆形

  • 椭圆的话直接改变Rect就行了
#pragma mark - 画圆
-(void)drawCiclePath:(CGRect)rect{
    
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, rect.size.width - 20, rect.size.height - 20)];
    
    path.lineWidth = 2;
    
    UIColor *fillColor = [UIColor redColor];
    [fillColor set];
    [path fill];
    
    UIColor *strokeColor = fillColor;
    [strokeColor set];
    
    [path stroke];
    
}

3、矩形

#pragma mark - 画矩形
-(void)drawRectPath:(CGRect)rect{
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, rect.size.width, rect.size.height)];
    
    path.lineWidth = 2;
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    
    UIColor *fillColor = [UIColor redColor];
    [fillColor set];
    [path fill];
    
    UIColor *strokeColor = fillColor;
    [strokeColor set];
    
    [path stroke];
    
}

圆角矩形系统提供了两种方法

  • 1、+(instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;
  • 2、+(instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
  • 使用方法是替换下面的一行代码
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, rect.size.width, rect.size.height)];

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, rect.size.width, rect.size.height) cornerRadius:10];

或者

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, rect.size.width, rect.size.height) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)];
  • 其中byRoundingCorners传入的值为系统提供的一个枚举,分别支持五种形式
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
    UIRectCornerTopLeft     = 1 << 0,
    UIRectCornerTopRight    = 1 << 1,
    UIRectCornerBottomLeft  = 1 << 2,
    UIRectCornerBottomRight = 1 << 3,
    UIRectCornerAllCorners  = ~0UL
};

4、调用

  • 三个方法要在-(void)drawRect:(CGRect)rect;方法中调用
-(void)drawRect:(CGRect)rect{
//    [self drawTrianglePath:rect];
//    [self drawRectPath:rect];
    [self drawCiclePath:rect];
}

5、全剧终

相关文章

网友评论

      本文标题:iOS 画三角形、圆形、矩形,一看就会那种

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