Quartz2D-基本图形绘制

Quartz2D简单实用

//
//  GRedView.m
//  Quartz2D-基本图形绘制
//
//  Created by gaocai on 16/7/27.
//  Copyright © 2016年 gaocai. All rights reserved.
//

#import "GRedView.h"

@implementation GRedView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {

    CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
    CGFloat radius = rect.size.width * 0.5 - 10;
    CGFloat start = 0;
    CGFloat end = M_PI;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:start endAngle:end clockwise:YES];
    //内部自动调用上下文
//    [path stroke];
    [path fill];
}



- (void)drawRect {
    
    //1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.获取描述路径
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)];
    //3.吧路径添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    //4.将上下文的内容显示到View上
    CGContextFillPath(ctx);
}


/**
 *  画曲线
 */
- (void)drawQuadCurve {
    //1获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2获取描述路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    //设置起点
    [path moveToPoint:CGPointMake(20, 100)];
    //添加另一点还有一个控制点
    [path addQuadCurveToPoint:CGPointMake(200, 200) controlPoint:CGPointMake(100, 10)];
    //3把路径添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    //把上下文的内容显示到View上
    CGContextStrokePath(ctx);

}



/**
 *  画线
 */
- (void)drawLine{
    //1首先取得上下文
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    
    //2其次获得一个描述路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    //设置线的起点
    [path moveToPoint:CGPointMake(10, 100)];
    //链接线的第二个点
    [path addLineToPoint:CGPointMake(200, 20)];
    //当添加第三个点时会把第二个点当成起始点
    [path addLineToPoint:CGPointMake(100, 200)];
    //同上
    [path addLineToPoint:CGPointMake(10, 100)];
    
    //设置线的宽度
    CGContextSetLineWidth(ctx, 10);
    //设置顶点的角为圆角
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    //设置起始点为圆角
    CGContextSetLineCap(ctx, kCGLineCapRound);
    //空心时的线颜色
    [[UIColor yellowColor] setStroke];
    //    [[UIColor yellowColor] setFill];//实心时线颜色 和CGContextFillPath对应
    
    
    //3然后将描绘的路径添加到上下文中
    CGContextAddPath(ctx, path.CGPath);
    
    //4最后渲染到View上
    CGContextStrokePath(ctx);//空心
    //    CGContextFillPath(ctx);//实心

}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值