zoukankan      html  css  js  c++  java
  • 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
    时光见证了成长,还很无知,我想一点点幼稚转为有知!
  • 相关阅读:
    面试题目小结
    面试题目3
    C#中new和override区别
    [转]:存储过程与函数的区别
    [转载]:C# 面试题大全
    [转]:C++虚函数表解析
    【修订版】C#/ASP.Net 面试题及答案(1)
    [转载]:C# 中结构与类的区别
    [转载]:C#笔试题面试题锦集
    [转载]:SQL Server性能调优之执行计划深度剖析 第一节 浅析SQL执行的过程
  • 原文地址:https://www.cnblogs.com/foreveriOS/p/5403836.html
Copyright © 2011-2022 走看看