• 02-CALayer(中和)


    //
    //  ViewController.m
    //  01-CALayer创建
    //
    //  Created by mac on 16/4/18.
    //  Copyright © 2016年 mac. All rights reserved.
    //
    /*
     1. position:确定当前图层的锚点到父视图层坐标到原点的相对偏移量,在当前图层上找出锚点位置,将两者对齐
     2. 绘制直线三部曲:创建可变路径(pathCreateMutable) : 添加到context(addPath) :开始绘制(drawPath)
                     途径阶段2:起始点确定,  属性设置(线宽和颜色)
     */
    
    #import "DrawLayer.h"
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController {
        
        DrawLayer *layer;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [self creatCALayer];
    }
    
    /**
     *  创建CALayer
     */
    ///*
    - (void)creatCALayer {
        
        CALayer *layer = [CALayer layer];
        
        layer.bounds = CGRectMake(0, 0, 200, 200); //position和锚点决定位置,bounds决定大小
    //    layer.frame = CGRectMake(0, 0, 200, 200); //frame属性对于layer的约束只是大小约束,位置是由anchorPoint决定
        layer.backgroundColor = [UIColor orangeColor].CGColor;
        
        [self.view.layer addSublayer:layer];
        
        layer.position = CGPointMake(100, 300);
        layer.anchorPoint = CGPointZero;
    }//*/
    
    ///*
     - (void)creatCALayer {
     
     CALayer *layer = [CALayer layer];
     
     layer.bounds = CGRectMake(0, 0, 200, 200); //position和锚点决定位置,bounds决定大小
     layer.backgroundColor = [UIColor orangeColor].CGColor;
     layer.anchorPoint = CGPointZero;
     
     //使用代理对象来进行绘图
     layer.delegate = self;
     [self.view.layer addSublayer:layer];
     
     //让图层强制从新绘制
     [layer setNeedsDisplay];
     }
     //*/
    
    - (void)creatCALayer {
        
        layer = [[DrawLayer alloc] init];
        
        layer.bounds = CGRectMake(0, 0, 200, 200); //position和锚点决定位置,bounds决定大小
        layer.backgroundColor = [UIColor orangeColor].CGColor;
        layer.anchorPoint = CGPointZero;
        
        //使用代理对象来进行绘图
        layer.delegate = self;
        [self.view.layer addSublayer:layer];
        
        //让图层强制从新绘制
        [layer setNeedsDisplay];
    }
    
    /**
     *  layer的代理方法。只有设置了代理才能调用此方法
     */
    ///*
    - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
        
    //    NSLog(@"hahahha");
        
        CGMutablePathRef path = CGPathCreateMutable(); //1. 在图层中绘制一条线 path获取
        
        CGPathMoveToPoint(path, NULL, 50, 50); //2. 起点
        CGPathAddLineToPoint(path, NULL, 150, 150);//连线点
        
    //    [[UIColor yellowColor] setStroke]; //CALayer 不是UIKit框架中的类,所有不能调用UIKit中的相关接口
        CGContextSetRGBStrokeColor(ctx, 0, 1, 1, 1);//3. stoke线颜色
        CGContextSetLineWidth(ctx, 5);//线宽
        
        CGContextAddPath(ctx, path);//4. path添加到context
        CGContextDrawPath(ctx, kCGPathStroke);//5. 绘制
    }//*/
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        
        UITouch *touch = [touches anyObject];
        CGPoint currentPoint = [touch locationInView:touch.view];
        
    //    layer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);
        layer.transform = CATransform3DRotate(layer.transform, M_PI_4, 0, 0, 1); //注意make和非make区别
        layer.position = currentPoint;
        layer.borderWidth = 20;
        layer.borderColor = [UIColor blueColor].CGColor;
        layer.opacity = .5;
    }
    
    @end
    时光见证了成长,还很无知,我想一点点幼稚转为有知!
  • 相关阅读:
    随机点名系统
    JQuery
    百度搜索下拉提示
    正则表达式
    严格模式
    CSS引入方式有哪些,区别是什么
    Js中的函数
    float浮动造成高度塌陷的解决办法
    PC端页面开发基础-问题总结(一)
    PC端页面开发基础-IE6常见CSS解析Bug及Hack
  • 原文地址:https://www.cnblogs.com/foreveriOS/p/5403836.html
Copyright © 2020-2023  润新知