源码0309-雪花(定时器)-图形上下文状态栈-矩阵操作

雪花定时器

//  DrawView.m
//  09-雪花(定时器)
#import "DrawView.h"
static CGFloat _snowY = 0;
@implementation DrawView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    
    // 如果以后想绘制东西到view上面,必须在drawRect方法里面,不管有没有手动获取到上下文
 
    // 修改雪花y值
    UIImage *image =  [UIImage imageNamed:@"雪花"];
    
    [image drawAtPoint:CGPointMake(50, _snowY)];
    
    _snowY += 10;
    
    if (_snowY > rect.size.height) {
        _snowY = 0;
    }

}

// 如果在绘图的时候需要用到定时器,通常

// NSTimer很少用于绘图因为调度优先级比较低并不会准时调用
- (void)awakeFromNib
{
    // 创建定时器
//    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(timeChange)];
    
    // 添加主运行循环
    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}

// CADisplayLink:每次屏幕刷新的时候就会调用,屏幕一般一秒刷新60次

// 1秒 2次
static int count = 0;
- (void)timeChange
{
//    count++;
//    if (count % 30) {// 一秒钟调用2次
//        
//    }
    
    
    // 注意:这个方法并不会马上调用drawRect,其实这个方法只是给当前控件添加刷新的标记,等下一次屏幕刷新的时候才会调用drawRect
    [self setNeedsDisplay];
}


@end

10-图形上下文状态栈

//  DrawView.m
//  10-图形上下文状态栈
#import "DrawView.h"

@implementation DrawView

// 如果以后用贝瑟尔绘制图形【path stroke】,上下文的状态由贝瑟尔路径状态
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    // 1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 2.描述路径
    // 第一根
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    [path moveToPoint:CGPointMake(10, 125)];
    
    [path addLineToPoint:CGPointMake(240, 125)];
    
    // 把路径添加到上下文
    // .CGPath 可以UIkit的路径转换成CoreGraphics路径
    CGContextAddPath(ctx, path.CGPath);
    
    // 保存一份上下文的状态
    CGContextSaveGState(ctx);
    
    
    
    // 设置上下文状态
    CGContextSetLineWidth(ctx, 10);
    
    [[UIColor redColor] set];
   
    // 渲染上下文
    CGContextStrokePath(ctx);
    
    // 第二根
    
   
    
    // 2.描述路径
    // 第一根
    path = [UIBezierPath bezierPath];
    
    [path moveToPoint:CGPointMake(125, 10)];
    
    [path addLineToPoint:CGPointMake(125, 240)];
    
    // 把路径添加到上下文
    // .CGPath 可以UIkit的路径转换成CoreGraphics路径
    CGContextAddPath(ctx, path.CGPath);
    
    // 还原状态
    CGContextRestoreGState(ctx);
//    // 设置上下文状态
//    CGContextSetLineWidth(ctx, 1);
//    
//    [[UIColor blackColor] set];
    
    // 渲染上下文
    CGContextStrokePath(ctx);
    
}


@end

11-矩阵操作

//  DrawView.m
//  11-矩阵操作
#import "DrawView.h"
@implementation DrawView
- (void)drawRect:(CGRect)rect {
    
    // 1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 2.描述路径
   UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-100, -50, 200, 100)];
    
    [[UIColor redColor] set];
    
    // 上下文矩阵操作
    // 注意:矩阵操作必须要在添加路径之前
    
    //  平移
    CGContextTranslateCTM(ctx, 100, 50);
    
    // 缩放
    CGContextScaleCTM(ctx, 0.5, 0.5);
    
    // 旋转
    
    CGContextRotateCTM(ctx, M_PI_4);
    
    // 3.把路径添加上下文
    CGContextAddPath(ctx, path.CGPath);
 
    [[UIColor redColor] set];
    
    // 4.渲染上下文
    CGContextFillPath(ctx);
    
}
@end
本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
原文地址:https://www.cnblogs.com/laugh/p/6677561.html