打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
同一个页面多个CALayer重绘的办法[原创]

//知识点,CALayer的重绘,-(void)drawLayer:(CALayer *)layerinContext:(CGContextRef)ctx 方法,CALayer的渐变色。多个CALayer重绘的方法。

//本例是一个,ViewController类,没有继承任何delegate,


也就是说下边的ca1,ca2,ca3的delegate直接设置为self,不用继承像<UITextFieldDelegate>这样的委托。CALyer好像没有委托,直接用就是了。不用搞清楚,直接写就是了。


//测试这个例子的随便建一个基于UIViewController类的,我选的是SingleViewApplication如图:


独立的视图应用)第一排的第四个,随便起个名字,进去将所有的AnimationController(我的项目名)改成你自己的项目的名字。就可以运行了。

//还得添加一个QuartzCore.framework,怎么添加就不说了,已经说的很清楚了,哪里不会留言...

//怎么添加QuartzCore.framework还是说下吧:

//1,在左侧点击点击项目

//2,中间的位置往上拖一点,看到四个黄色的小工具箱,左下角有个加号,点击出来很多个黄色的小箱子,找到QuartzCore.framework点Add。就添加进来了,然后在m文件中声明,这都是必要的过程,就像我的m文件里的#import<QuartzCore/QuartzCore.h>。





//h文件:

 

#import<UIKit/UIKit.h>


@interface AnimationController: UIViewController

//注意UIViewController后边没有<委托>;

@property(nonatomic, retain)CALayer *layer1;

@property (nonatomic,retain)CALayer *layer2;

@property (nonatomic,retain)CALayer *layer3;

//定义三个CALayer*layer1,*layer2,*layer3;

@end


 

//M文件:

 

 

#import"AnimationController.h"

#import<QuartzCore/QuartzCore.h>

@interfaceAnimationController ()

#defineUIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue& 0xFF0000)>> 16))/255.0 green:((float)((rgbValue& 0xFF00)>> 8))/255.0 blue:((float)(rgbValue& 0xFF))/255.0 alpha:1.0]

//这个地方是一个常量,用来设置RGB颜色,不用可以不写,我的例子用了,所以请你照抄,蛮有用处的,留着也可以以后用。


@end


@implementationAnimationController


@synthesizelayer1 = _layer1;

@synthesizelayer2 = _layer2;

@synthesizelayer3 = _layer3;

//上边是3个CALayer类_layer1,_layer2,_layer3,就是h文件中的 *layer1; *layer2; *layer3,这个是IOS开发的机制,不解释;


-(void)viewDidLoad

{

  [super viewDidLoad];  


   self.view.layer.backgroundColor = [UIColor whiteColor].CGColor;

//设置self.view.layer背景色。


    CALayer *ca1= [CALayer layer];

//实例化一个CALayer:ca1;

       ca1.backgroundColor = [UIColor whiteColor].CGColor;

//设置ca1的背景颜色。

    ca1.frame =CGRectMake(50, 500, 200, 200);

//设置ca1的frame(坐标和大小);

   ca1.delegate = self;

//ca1.delegate = self;这一步非常重要,

   ca1.name = @"fuck";

//这个ca1.name设置不设置都行,没有用到。

   [ca1 setNeedsDisplay];

//ca1调用setNeedsDisplay,来绘制自己的页面。


   CALayer *ca2 = [CALayer layer];

   ca2.backgroundColor = [UIColor orangeColor].CGColor;

   ca2.frame = CGRectMake(300, 500, 200, 200);

   ca2.name = @"you";

   ca2.delegate = self;

   [ca2 setNeedsDisplay];

//ca2的和ca1没什么区别,就是颜色和坐标不同,还有name不同。

   

   CALayer *ca3 = [CALayer layer];

   ca3.backgroundColor = [UIColor yellowColor].CGColor;

   ca3.frame = CGRectMake(550, 500, 200, 200);

   ca3.name = @"man";

   ca3.delegate = self;

   [ca3 setNeedsDisplay];

//ca3的和ca1没什么区别,就是颜色和坐标不同,还有name不同。   


   [self.view.layer addSublayer:ca1];

//向self.view.layer添加ca1,记住一定要是self.view的layer中添加。

   [self.view.layer addSublayer:ca2];

//向self.view.layer添加ca2

   [self.view.layer addSublayer:ca3];

//向self.view.layer添加ca3

   self.layer1 = ca1;

   //将ca1赋值给self.layer1;

   self.layer2 = ca2;

   //将ca2赋值给self.layer2;

   self.layer3 = ca3;

   //将ca2赋值给self.layer3;

}


//这个方法是很重要的:-(void)drawLayer:(CALayer *)layerinContext:(CGContextRef)ctx,传进来两个参数(CALayer *)类型的layer和(CGContextRef)类型的ctx,也就是说我们在调用[ca1 setNeedsDisplay]的时候他传进来的layer==ca1因为我们将ca1赋值给了self.layer1所以layer也==self.layer1,ctx是ca1这个层的上下文,也就相当于是一个画板,让我们在上边重绘,这个方法就相当于UIView的:-(void)drawRect;方法,能理解理解,不能理解你就知道他就是传进来两个参数,一个层,一个画板,在我们调用[ca1 setNeedsDisplay]的时候;

-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx

{

   NSLog(@"layer.name ==== %@",layer.name);

//输出当前的layer的name,可有可无,只是告诉我们当前是哪个layer。

    if(layer ==self.layer1)

//这里加一个判断,当layer是self.layer1的时候是一种重绘办法,也就是说三个层self.layer1,self.layer2,self.layer3你可能需要一个画树,一个画草,一个画小鸟,只是举个例子哦,我画不了小鸟的。我这里画的是,self.layer1:一个渐变的层,右上角画了一个灰色的三角符号,self.layer2:在一个层的右上角画了一个蓝色的三角符号,self.layer3:在一个层的右上角画了一个黑色的三角符号。

    {

       CGGradientRefmyGradient;

       CGColorSpaceRefmyColorSpace;

       size_t locationCount =3;

       CGFloat locationList[] ={0.0,0.1,1.0};

       CGFloatcolorList[]={

           1.0,0.0,0.5,1.0,

           1.0,0.0,1.0,1.0,

           0.3,0.5,1.0,1.0,

       };

       myColorSpace = CGColorSpaceCreateDeviceRGB();

        myGradient= CGGradientCreateWithColorComponents(myColorSpace,colorList, locationList, locationCount);//核心函数就是这个了,要搞清楚一些量化的东西了.

       CGPointstartPoint,endPoint;

       startPoint.x = 0;

       startPoint.y = 0;

       endPoint.x = CGRectGetMaxX(layer.bounds);

       endPoint.y = CGRectGetMaxY(layer.bounds);

       CGContextDrawLinearGradient(ctx,myGradient, startPoint, endPoint, 0);//这个是绘制的,你可以通过裁剪来完成特定形状的过度。

       CGColorSpaceRelease(myColorSpace);

       CGGradientRelease(myGradient);

       CGContextSetLineCap(ctx,kCGLineCapSquare);

        

       CGContextSetLineWidth(ctx,1.0);

        

       CGContextSetRGBStrokeColor(ctx,1.0, 0.0,0.0, 1.0);

//上边是设置层的渐变色的过程,代码具体我不讲了,没事自己研究研究IOS的一些绘画代码,以此为分割线,下边是画的一个简单的三角形。       


       CGContextBeginPath(ctx);

        //开始一个路径

       CGContextMoveToPoint(ctx,layer.bounds.size.width * 3/4,layer.bounds.origin.y + 1);

       //设置一个起点坐标

       CGContextAddLineToPoint(ctx,layer.bounds.size.width - 1, layer.bounds.origin.y + 1);

       //移动到下一个点的坐标

       CGContextAddLineToPoint(ctx,layer.bounds.size.width - 1, layer.bounds.size.width * 1/4);

       //移动到下一个点的坐标

       CGContextAddLineToPoint(ctx,layer.bounds.size.width * 3/4,layer.bounds.origin.y + 1);

       //移动到下一个点的坐标

        

       CGContextSetFillColorWithColor(ctx, UIColorFromRGB(0x555555).CGColor);

//设置填充色,这里就用到了我们上边定义的UIColorFromRGB()一般这个括号里输入的是(0x******)零x开头的后边是6位16进制的RGB颜色数,0x不变变换后边六位数就可以改变颜色,网上一搜一大把,或者我的博客里找有专门的RGB颜色网站集合。

       CGContextFillPath(ctx);

       //填充.

        

       CGContextStrokePath(ctx);

//连接所有点,就成了一个三角形了。

    }

    if(layer ==self.layer2)

    {

       CGContextSetLineCap(ctx,kCGLineCapSquare);

        

       CGContextSetLineWidth(ctx,1.0);

        

       CGContextSetRGBStrokeColor(ctx,1.0, 0.0,0.0, 1.0);

        

       CGContextBeginPath(ctx);

        

       CGContextMoveToPoint(ctx,layer.bounds.size.width * 3/4,layer.bounds.origin.y + 1);

       //设置一个起点坐标

       CGContextAddLineToPoint(ctx,layer.bounds.size.width - 1, layer.bounds.origin.y + 1);

       //移动到下一个点的坐标

       CGContextAddLineToPoint(ctx,layer.bounds.size.width - 1, layer.bounds.size.width * 1/4);

       //移动到下一个点的坐标

       CGContextAddLineToPoint(ctx,layer.bounds.size.width * 3/4,layer.bounds.origin.y + 1);

       //移动到下一个点的坐标

        

       CGContextSetFillColorWithColor(ctx, UIColorFromRGB(0x8F9FEA).CGColor);

       CGContextFillPath(ctx);

       CGContextStrokePath(ctx);

//这个和self.layer1的画三角一样,只是我改变了他的RGB便于区别。

       //把这些坐标连起来

    }

    if(layer ==self.layer3)

    {

       CGContextSetLineCap(ctx,kCGLineCapSquare);

        

       CGContextSetLineWidth(ctx,1.0);

        

       CGContextSetRGBStrokeColor(ctx,1.0, 0.0,0.0, 1.0);

        

       CGContextBeginPath(ctx);

        

       CGContextMoveToPoint(ctx,layer.bounds.size.width * 3/4,layer.bounds.origin.y + 1);

       //设置一个起点坐标

       CGContextAddLineToPoint(ctx,layer.bounds.size.width - 1, layer.bounds.origin.y + 1);

       //移动到下一个点的坐标

       CGContextAddLineToPoint(ctx,layer.bounds.size.width - 1, layer.bounds.size.width * 1/4);

       //移动到下一个点的坐标

       CGContextAddLineToPoint(ctx,layer.bounds.size.width * 3/4,layer.bounds.origin.y + 1);

       //移动到下一个点的坐标

        

       CGContextSetFillColorWithColor(ctx, UIColorFromRGB(0x000000).CGColor);

       CGContextFillPath(ctx);

        

        

       CGContextStrokePath(ctx);

//这个和self.layer2的画三角一样,也只是改变了颜色。

    }

}

@end

运行结果是这样的:
弄了很久,想方便你们,拿走请注明出处。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
彻底理解position与anchorPoint
【投稿】使用CALayer的Mask实现注水动画效果
IOS 特性CALayer的使用
iOS开发中CAlayer层的属性以及自定义层的方法
一分钟学会环形进度条 | gcblog
Core Animation advanced Techniques 学习笔记(1)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服