美文网首页
UIBezierPath使用

UIBezierPath使用

作者: iOS_Xue | 来源:发表于2016-08-02 16:44 被阅读27次

    如果需要自己画图案,新建一个UIView子类,在- (void)drawRect:(CGRect)rect方法中进行绘制。

    类方法

    /**
     *  最常用的方法
     */
    //+ (instancetype)bezierPath;
    /**
     *  画矩形方法
     */
    //+ (instancetype)bezierPathWithRect:(CGRect)rect;
    /**
     *  画圆形、椭圆方法
     */
    //+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
    /**
     *  画圆角矩形
     *  @param rect         矩形尺寸
     *  @param cornerRadius 圆角设置
     */
    //+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;
    /**
     *  画某几个角为圆角的矩形
     *
     *  @param rect        矩形尺寸
     *  @param corners     设置圆角的位置
     *  @param cornerRadii 圆角设置
     */
    //+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
    /**
     *  画弧
     *
     *  @param center     圆心
     *  @param radius     半径
     *  @param startAngle 起始角度
     *  @param endAngle   结束角度
     *  @param clockwise  是否顺时针画图
     */
    //+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
    

    实例方法

    /**
     *  设置线段起点
     */
    //- (void)moveToPoint:(CGPoint)point;
    /**
     *  把某点加入到线段
     */
    //- (void)addLineToPoint:(CGPoint)point;
    /**
     *  画三次贝塞尔线,需使用moveToPoint先设置起点。
     *
     *  @param endPoint      结束点
     *  @param controlPoint1 第一个控制点
     *  @param controlPoint2 第二个控制点
     */
    //- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
    /**
     *  画二次贝塞尔曲线,需使用moveToPoint先设置起点。
     *
     *  @param endPoint     结束点
     *  @param controlPoint 控制点
     */
    //- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
    /**
     *  画弧度
     *
     *  @param center     圆心
     *  @param radius     半径
     *  @param startAngle 起始角度
     *  @param endAngle   结束角度
     *  @param clockwise  是否顺时针画弧
     */
    //- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
    /**
     *  把结束点和起始点连起来,做闭合曲线。
     */
    //- (void)closePath;
    

    demo

    //三角形
    - (void)drawTrianglePath
    {
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(20, 20)];
        [path addLineToPoint:CGPointMake(self.frame.size.width - 20, 20)];
        [path addLineToPoint:CGPointMake(self.frame.size.width / 2, 200)];
    
        //闭合线条可以用下面的方法,也可以再调用addLineToPoint,添加起点坐标
        [path closePath];
        
        //线宽度
        path.lineWidth = 10.0;
        
        //线条拐角帽的样式
        path.lineCapStyle = kCGLineCapSquare;
        //两条线连接的样式
        path.lineJoinStyle = kCGLineJoinBevel;
        
        //填充色
        UIColor *fillColor = [UIColor grayColor];
        [fillColor set];
        [path fill];
        
        //线条色
        UIColor *strokeColor = [UIColor blueColor];
        [strokeColor set];
        
        [path stroke];
    }
    
    //矩形
    - (void)drawRectPath
    {
        UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, self.frame.size.width - 40, 200)];
        rectPath.lineWidth = 10.0;
        rectPath.lineCapStyle = kCGLineCapRound;
        rectPath.lineJoinStyle = kCGLineJoinBevel;
        
        UIColor *fillColor = [UIColor darkGrayColor];
        [fillColor set];
        [rectPath fill];
        
        UIColor *strokeColor = [UIColor orangeColor];
        [strokeColor set];
        
        [rectPath stroke];
    }
    
    //圆形
    - (void)drawCiclePath
    {
        UIBezierPath *ciclePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(90, 20, self.frame.size.width - 180, self.frame.size.width - 180)];
        
        ciclePath.lineWidth = 3.0;
        
        UIColor *fillColor = [UIColor colorWithRed:0.3 green:0.4 blue:0.3 alpha:1.0];
        [fillColor set];
        [ciclePath fill];
        
    //    UIColor *stroleColor = [UIColor redColor];
    //    [stroleColor set];
        
        [ciclePath stroke];
    }
    
    //圆角矩形
    - (void)drawCicleRectPath
    {
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, self.frame.size.width - 50, self.frame.size.height - 50) cornerRadius:5];
        path.lineWidth = 3;
        
        UIColor *fillColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0];
        [fillColor set];
        [path fill];
        
        UIColor *strokeColor = [UIColor blackColor];
        [strokeColor set];
        
        [path stroke];
    }
    
    - (void)drawCicleRectCustomPath
    {
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, self.frame.size.width - 50, self.frame.size.height - 50) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
        
        path.lineWidth = 20;
        
        UIColor *fillColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0];
        [fillColor set];
        [path fill];
        
        UIColor *strokeColor = [UIColor blackColor];
        [strokeColor set];
        
        [path stroke];
    }
    
    #define kDegreesToRadians(degrees) ((M_PI * degrees)/180)
    //画弧度
    - (void)drawARCPath
    {
        CGPoint center = self.center;
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:0 endAngle:kDegreesToRadians(88) clockwise:NO];
        //    path.lineCapStyle = kCGLineCapButt;
        path.lineJoinStyle = kCGLineJoinBevel;
        path.lineWidth = 10;
        
        UIColor *fillColor = [UIColor grayColor];
        [fillColor set];
        [path fill];
        
        
        UIColor *strokeColor = [UIColor blackColor];
        [strokeColor set];
        
        [path stroke];
    }
    
    //二次贝塞尔曲线
    - (void)drawSecondBezierPath
    {
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(100, 200)];
        [path addQuadCurveToPoint:CGPointMake(300, 200) controlPoint:CGPointMake(00, -100)];
        [path addQuadCurveToPoint:CGPointMake(350, 200) controlPoint:CGPointMake(350, -100)];
        
        path.lineWidth = 1;
        path.lineCapStyle = kCGLineCapRound;
        path.lineJoinStyle = kCGLineJoinRound;
        UIColor *strokeColor = [UIColor blackColor];
        [strokeColor set];
        
        [path stroke];
    }
    
    //三次贝塞尔曲线
    - (void)drawThirdBezierPath
    {
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(100, 50)];
        
        [path addCurveToPoint:CGPointMake(300, 200) controlPoint1:CGPointMake(-60, 400) controlPoint2:CGPointMake(200, -100)];
        
        path.lineWidth = 2;
        
        UIColor *fillColor = [UIColor grayColor];
        [fillColor set];
        [path fill];
        
        UIColor *strokeColor = [UIColor blackColor];
        [strokeColor set];
        
        [path stroke];
    }
    
    

    参考文章
    UIBezierPath精讲

    相关文章

      网友评论

          本文标题:UIBezierPath使用

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