BezierPath绘制

来源:互联网 发布:2017淘宝扣分清零规则 编辑:程序博客网 时间:2024/05/19 09:16
自定义曲线#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
0 0