美文网首页
画圆、矩形、椭圆

画圆、矩形、椭圆

作者: Z了个L | 来源:发表于2016-08-24 16:18 被阅读54次

// DrawView.h
#import <UIKit/UIKit.h>

@interface DrawView : UIView

@end

// DrawView.m

#import "DrawView.h"

@implementation DrawView


- (void)awakeFromNib {
    
    //描述一个椭圆
    //UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 200, 100)];
    //绘制路径
    //[path stroke];

}



- (void)drawRect:(CGRect)rect {
    
    //描述一个椭圆
    //UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 200, 200)];
    //绘制路径
    //[[UIColor yellowColor] set];
    //[path fill];
    
    //画弧
    //Center:弧所在的圆心
    //radius:圆的半径
    //startAngle:开始角度,圆的最右侧为0度
    //endAngle:截至角度,向下为正,向上为负.
    //clockwise:时针的方向,yes:顺时针 no:逆时针

//    NSLog(@"self.center=%@",NSStringFromCGPoint(self.center));
    
//    CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
//    CGFloat radius = rect.size.width * 0.5 - 10;
//    CGFloat startA = 0;
//    CGFloat endA = -M_PI_2;
//    //画弧的路径
//    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:NO];
//    
//    //画扇形
//    //添加一根线到圆心
//    [path addLineToPoint:center];
//    //关闭路径(自动的从路径的终点连接到路径的起点)
//    //[path closePath];
//    [[UIColor redColor] set];
//    //使用fill在填充之前,自动的关闭路径
//    [path fill];
    
    [self drawRect];
    //1.获取上下文->2.描述路径->3.把路径添加到上下文->4.把上下文的内容渲染到View的layer.
    
}

//画矩形
- (void)drawRect {
    //1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.描述路径
    //矩形
//    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 200, 200)];
    //画圆角矩形
    //cornerRadius:圆角半径
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 200, 200) cornerRadius:100];
    
    //3.把路径添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    CGContextSetLineWidth(ctx, 5);
    
    [[UIColor redColor] set];
    
    //4.把上下文的内容渲染到View的layer.
    CGContextStrokePath(ctx);
//    CGContextFillPath(ctx);
}


- (void)drawRect:(CGRect)rect {
//    [self drawRectOne];
    [self drawRectTwo];
}

// 一个椭圆两种方式
- (void)drawRectOne {
    //1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.描述路径
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 200, 100)];
    //3.把路径添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    //4.把上下文的内容渲染到View的layer.
    CGContextStrokePath(ctx);
}

// 一个椭圆两种方式
- (void)drawRectTwo {
    // 描述一个椭圆
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 200, 100)];
    //绘制路径
    [path stroke];
    // [path stroke];做了下面4步操作
    // 1.获取上下文->2.描述路径->3.把路径添加到上下文->4.把上下文的内容渲染到View的layer.
}


//drawAtPoint不会自动换行
//[str drawAtPoint:CGPointMake(0, 0) withAttributes:dict];
//drawInRect会自动换行
[str drawInRect:self.bounds withAttributes:dict];

// 当系统自动调用drawRect方法时会自动创建跟View相关联的上下文
// 重绘setNeedsDisplay系统会自动调用drawRect:
[self setNeedsDisplay];
@end


  • 两种方式都能画出椭圆,效果是一样的,只不过第二种方式更简单,但是第二种方式的[path stroke];实际上是做了4步操作的,调用第二种方法只能在drawRect:方法里面,不然没有效果,并且会报错。
  • 效果图显示:

相关文章

  • 画圆、矩形、椭圆

    两种方式都能画出椭圆,效果是一样的,只不过第二种方式更简单,但是第二种方式的[path stroke];实际上是做...

  • UIBezierPath

    UIBezierPath 画线 画圆 画曲线 画矩形 绘制圆角矩形 绘制椭圆

  • 自定义视图

    画圆 画椭圆 矩形 多边形 画图片

  • php使用gd库在图片中画圆角矩形

    最近需要在图片中画椭圆,但是gd库函数有矩形、圆弧、椭圆……就是没有圆角矩形然后在网上找的画圆角矩形的方法试了一些...

  • 04-显示图形

    1.画矩形 2.画曲线 3.画圆 画椭圆 将内容展示在屏幕上

  • Android Canvas 方法总结(五)

    画点: 画线 矩形 多边形 曲线 画圆 画椭圆 画弧度 画图片 画文字 bottom top可以通过 Paint....

  • 第一周任务情况

    学会了哪些 工具 Inkscape 画圆,椭圆,矩形,长方形,五角星等等,按住 Ctrl + 鼠标拖动是正型。 线...

  • ios绘图基础

    ios常见的图形绘制 画线 画圆、圆弧 画矩形,画椭圆,多边形 画图片 画文字 1:ios绘图基础 几个基本的概念...

  • Quartz2D--基本画图(线、图形、图片、文字)

    画直线 添加曲线 画矩形 画椭圆 画圆的弧线->扇形->圆 将图片直接画在view上 画文字 图文上下文栈 上下文...

  • canvas

    canvas是html5新增的标签:1.兼容性:IE9+;2.简单用法包括画线,画矩形,画椭圆,画圆;3.canv...

网友评论

      本文标题:画圆、矩形、椭圆

      本文链接:https://www.haomeiwen.com/subject/urlosttx.html