TIANKONGHAIHUO

导航

UIKit绘图方法

Main.storyboard

 

CustomView.h

#import <UIKit/UIKit.h>

 

@interface CustomView : UIView

 

@end

//

//  CustomView.m

//  5B03.UIKit绘图方法

//

//  Created by huan on 16/1/29.

//  Copyright © 2016 huanxi. All rights reserved.

//

 

#import "CustomView.h"

 

@implementation CustomView

 

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

 */

- (void)drawRect:(CGRect)rect {

//    // Drawing code

//    //以前一直都要获取上下文,

//    //UIKit的方法,虽然不用获取上下文,但内部最终都会获取上下文绘制

//    //使用UIKit绘图方法

//    //画实心

//    UIRectFill(CGRectMake(10, 10, 100, 100));

//    //画空心

//    UIRectFrame(CGRectMake(10, 110, 50, 50));

    

    CGContextRef ctx = UIGraphicsGetCurrentContext();

//    //每调用一次,往上下文添加路径

//    CGContextMoveToPoint(ctx, 10, 10);

//    CGContextAddLineToPoint(ctx, 100, 100);

    

    //先把所有的路径定义好,然后一次往往上下文中添加

    CGMutablePathRef path = CGPathCreateMutable();

    //设置圆的路径

    CGPathAddEllipseInRect(path, nil, CGRectMake(10, 10, 100, 100));

    CGPathAddEllipseInRect(path, nil, CGRectMake(20, 20, 80, 80));

    

    //添加弧的路径

//    CGPathAddArc(<#CGMutablePathRef  _Nullable path#>, <#const CGAffineTransform * _Nullable m#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFloat endAngle#>, <#bool clockwise#>);

    

    

    //添加线路径

//    CGPathAddLines(<#CGMutablePathRef  _Nullable path#>, <#const CGAffineTransform * _Nullable m#>, <#const CGPoint * _Nullable points#>, <#size_t count#>)

    //把路径添加到上下文

    CGContextAddPath(ctx, path);

    //渲染

    CGContextStrokePath(ctx);

    

    //开发过程中,ARC环境 c语言的资源不会自动释放

    //什么情况下创建的c语言资料,需要释放 create,retain,copy创建的数据要释放

//    CGPathRelease(path);

    //通用

    CFRelease(path);

}

 

 

@end

posted on 2016-02-02 15:40  日月行程  阅读(225)  评论(0编辑  收藏  举报