码迷,mamicode.com
首页 > 其他好文 > 详细

绘制垃圾图像

时间:2015-10-02 19:50:41      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:


#import
"MyLayer.h" @implementation MyLayer - (void)drawInContext:(CGContextRef)ctx { // 设置填充色 CGContextSetRGBFillColor(ctx, 1, 0, 0, 1); // 椭圆 CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 50, 50)); // 填充路径 CGContextFillPath(ctx); } @end
#import "MyView.h"

@implementation MyView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    // 设置rgb填充颜色
    CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
    // 矩形内部添加椭圆
    CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 50, 50));
    // 填充路径
    CGContextFillPath(ctx);
}

// UIView *view;

    

// view.layer.delegate == view;

    

// view的完整显示过程

// 1. view.layer会准备一个Layer Graphics Contex(图层类型的上下文)

// 2. 调用view.layer.delegate(view)drawLayer:inContext:,并传入刚才准备好的上下文

// 3. viewdrawLayer:inContext:方法内部又会调用viewdrawRect:方法

// 4. view就可以在drawRect:方法中实现绘图代码, 所有东西最终都绘制到view.layer上面

// 5. 系统再将view.layer的内容拷贝到屏幕, 于是完成了view的显示

 

#import "ViewController.h"
#import "MyView.h"
#import "MyLayer.h"

@interface ViewController ()


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CALayer *layer = [CALayer layer];
    //大小
    layer.bounds = CGRectMake(0, 0, 100, 100);
    //颜色
    layer.backgroundColor = [UIColor blackColor].CGColor;
    //锚点
    layer.anchorPoint = CGPointZero;
    //位置
    layer.position = CGPointMake(100, 100);
    //代理
    layer.delegate = self;
    [layer setNeedsDisplay];
    [self.view.layer addSublayer:layer];
    
    
    [self diyLayer];
      
    
}
#pragma mark - 图层的代理方法
//绘制代理
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
    CGContextAddRect(ctx, CGRectMake(0, 0, 20, 20));
    CGContextFillPath(ctx);
}

- (void)diyLayer
{
    MyLayer *layer = [MyLayer layer];
    layer.bounds = CGRectMake(0, 0, 100, 100);
    layer.backgroundColor = [UIColor blueColor].CGColor;
    layer.anchorPoint = CGPointZero;
    [layer setNeedsDisplay];
    [self.view.layer addSublayer:layer];
}

@end

 

绘制垃圾图像

标签:

原文地址:http://www.cnblogs.com/coderMJL/p/4852555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!