BezierPath绘制


自定义曲线
#import <UIKit/UIKit.h>

typedef enum{
    BrokenLineType,折线类型
    RectangleType,矩形(正方形)
    RoundType,圆形(椭圆)、圆环
    ArcType,///弧线(扇形)
    CurveType,曲线
}PathType;
@interface NewView : UIView

@end


#import "NewView.h"

@interface NewView()
@property (nonatomic, assign)PathType pathType;

@end
@implementation NewView

/*
 自定义视图的时候可以通过drawRect方法绘制,在需要更新绘制的时候可以通过[self setNeedsDisplay];来重新绘制,最好在自定义视图类内部完成,

 */
- (void)setBezierType:(PathType)pathType
{
    _pathType = pathType;
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect
{
    /*
     ///把自定义视图上面的绘制线全部填充覆盖掉
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor);
    CGContextFillRect(context, CGRectMake(0, 0, 0, 0));作用范围
     */

    [super drawRect:rect];
    UIColor *redColor = [UIColor redColor];
    [redColor set];


    switch (_pathType) {
        case BrokenLineType:{///绘制折线
            CGPoint startPoint = CGPointMake(33, 33);
            CGPoint stopPoint = CGPointMake(57, 68);
            CGPoint nextPoint = CGPointMake(80, 40);
            UIBezierPath *path = [UIBezierPath bezierPath];
            [path moveToPoint:startPoint];
            [path addLineToPoint:stopPoint];
            [path addLineToPoint:nextPoint];

            path.lineCapStyle = kCGLineCapRound; //线条拐角
            path.lineJoinStyle = kCGLineCapRound; //终点处理
            path.lineWidth = 1;设置线宽
            //    [path stroke];根据坐标点连线
            [path closePath];
            [path fill];这个是直接填充 和stroke相对应
            break;
        }
        case RectangleType:{绘制矩形
            UIBezierPath *juXing = [UIBezierPath bezierPathWithRect:CGRectMake(50, 80, 30, 40)];
            [redColor set];
            juXing.lineWidth = 0.5;
            juXing.lineCapStyle = kCGLineCapRound; //线条拐角
            juXing.lineJoinStyle = kCGLineCapRound; //终点处理
            [juXing stroke];需要连线
            break;
        }
        case RoundType:{绘制圆或者椭圆
            /* bezierPathWithOvalInRect:<#(CGRect)#>
             此参数传矩形方框绘制的就是矩形方框的内切椭圆------传入正方形绘制的就是内切圆
             */
            UIBezierPath *yuan = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 80, 30, 40)];
            [redColor set];
            yuan.lineWidth = 0.5;
            [yuan stroke];需要连线
            break;
        }
        case ArcType:{///绘制弧线或者扇形
            CGPoint centerPoint = CGPointMake(150, 150);
            UIBezierPath* huXian = [UIBezierPath bezierPathWithArcCenter:centerPoint///中心点
                                                                  radius:75///半径
                                                              startAngle:0///开始角度
                                                                endAngle:M_PI*2/3.0结束角度
                                                               clockwise:YES];
            huXian.lineWidth = 5.0;
            huXian.lineCapStyle = kCGLineCapRound; //线条拐角
            huXian.lineJoinStyle = kCGLineCapRound; //终点处理
            [huXian addLineToPoint:centerPoint];
            [huXian closePath];
            [huXian fill];
            //    [huXian stroke];

            break;
        }
        case CurveType:{绘制二/三次贝塞尔曲线
            UIBezierPath *erCiBe = [UIBezierPath bezierPath];

            erCiBe.lineWidth = 1.0;
            erCiBe.lineCapStyle = kCGLineCapRound; //线条拐角
            erCiBe.lineJoinStyle = kCGLineCapRound; //终点处理

            CGPoint startPoint = CGPointMake(40, 80);
            CGPoint stopPoint = CGPointMake(160, 120);

            CGPoint controlPoint = CGPointMake(70, 50);
            [erCiBe moveToPoint:startPoint];
            绘制二次
            [erCiBe addQuadCurveToPoint:stopPoint controlPoint:controlPoint];
            CGPoint controlPoint0 = CGPointMake(80, 90);
            ///绘制三次
            //    [erCiBe addCurveToPoint:stopPoint controlPoint1:controlPoint controlPoint2:controlPoint0];

            [erCiBe stroke];
            break;
        }
        default:
            break;
    }

}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值