BezierPath繪製

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/qq_28125515/article/details/50401710

自定義曲線
#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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章