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
时光见证了成长,还很无知,我想一点点幼稚转为有知!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步