iOS Quartz繪圖

//
//  DrawView.m
//  Demo
//
//  Created by llkj on 2017/8/21.
//  Copyright © 2017年 LayneCheung. All rights reserved.
//

#import "DrawView.h"

@implementation DrawView


- (void)drawRect:(CGRect)rect {

    //畫弧
    //Center:弧所在的圓心
    //radius:圓的半徑
    //startAngle:開始角度
    //endAngle:截至角度
    //clockwise: YES:順時針 NO:逆時針

    NSLog(@"%@",NSStringFromCGPoint(self.center));

    CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
    CGFloat radius = rect.size.width * 0.5 - 10;
    //不能直接會用self.center ,是因爲self.center座標是相對於它的父控件.
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:-M_PI_2 clockwise:NO];

    //添加一根線到圓心
    [path addLineToPoint:center];

    //關閉路徑closePath:從路徑終點連接一根線到路徑的起點
    //[path closePath];

    [[UIColor redColor] set];

    //畫扇形
    //fill(填充之前,會自動關閉路徑)
    [path fill];
}

//畫橢圓
- (void)drawOval{

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 50)];

    [[UIColor orangeColor] set];
    [path stroke];//1.獲取上下文->2.繪製路徑->3.把繪製的內容添加到上下文中->4.把上下文添加到View上
    //[path fill];

}

//畫直線
- (void)drawLine{

    //1.獲取上下文
    CGContextRef ref = UIGraphicsGetCurrentContext();

    //2.繪製路徑
    UIBezierPath *path = [UIBezierPath bezierPath];

    //2.1設置起點
    [path moveToPoint:CGPointMake(50, 250)];

    //2.2添加一根線到終點
    [path addLineToPoint:CGPointMake(250, 50)];

    [path addLineToPoint:CGPointMake(100, 280)];

    //上下文的狀態

    //設置線的寬度
    CGContextSetLineWidth(ref, 10);

    //設置線的連接樣式
    CGContextSetLineJoin(ref, kCGLineJoinRound);

    //設置線的頂角樣式
    CGContextSetLineCap(ref, kCGLineCapRound);

    //設置顏色
    [[UIColor redColor] setStroke];

    //3.把繪製的內容添加到上下文中
    CGContextAddPath(ref, path.CGPath);

    //4.把上下文添加到View上
    CGContextStrokePath(ref);
}

//畫矩形
- (void)drawRect{

    CGContextRef ref = UIGraphicsGetCurrentContext();

    //    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 200, 100)];矩形

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 100, 100) cornerRadius:100];//圓角矩形

    [[UIColor orangeColor] set];

    CGContextAddPath(ref, path.CGPath);

    //    CGContextStrokePath(ref);//描邊
    CGContextFillPath(ref);//填充

}

//畫曲線
- (void)drawQuadCurve{

    CGContextRef ref = UIGraphicsGetCurrentContext();

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(50, 280)];

    [path addQuadCurveToPoint:CGPointMake(250, 280) controlPoint:CGPointMake(50, 50)];

    CGContextAddPath(ref, path.CGPath);

    CGContextStrokePath(ref);

}

@end
發佈了55 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章