iOS開發(fā)中CAlayer層的屬性以及自定義層的方法
CAlayer層的屬性
一、position和anchorPoint
1.簡單介紹
CALayer有2個(gè)非常重要的屬性:position和anchorPoint
@property CGPoint position;
用來設(shè)置CALayer在父層中的位置
以父層的左上角為原點(diǎn)(0, 0)
@property CGPoint anchorPoint;
稱為“定位點(diǎn)”、“錨點(diǎn)”
決定著CALayer身上的哪個(gè)點(diǎn)會(huì)在position屬性所指的位置
以自己的左上角為原點(diǎn)(0, 0)
它的x、y取值范圍都是0~1,默認(rèn)值為(0.5, 0.5)
2.圖示
anchorPoint
它的取值為0~1
紅色圖層的anchorPoint為(0,0)
紅色圖層的anchorPoint為(0.5,0.5)
紅色圖層的anchorPoint為(1,1)
紅色圖層的anchorPoint為(0.5,0)
position和anchorPoint
添加一個(gè)紅色圖層到綠色圖層上,紅色圖層顯示到什么位置,由position屬性決定
假設(shè)紅色圖層的position是(100,100)
到底把紅色圖層的哪個(gè)點(diǎn)移動(dòng)到(100,100)的坐標(biāo)位置,錨點(diǎn)。
紅色圖層的錨點(diǎn)是(0,0)
紅色圖層的錨點(diǎn)是(0.5,0.5)
紅色圖層的錨點(diǎn)是(1,1)
紅色圖層的錨點(diǎn)是(0.5,0)
3.代碼示例
(1)沒有設(shè)置錨點(diǎn)。默認(rèn)的錨點(diǎn)位置為(0.5,0.5)
//
// YYViewController.m
// 03-錨點(diǎn)等屬性
//
// Created by apple on 14-6-21.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import "YYViewController.h"
@interface YYViewController ()
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//創(chuàng)建圖層
CALayer *layer=[CALayer layer];
//設(shè)置圖層的屬性
layer.backgroundColor=[UIColor redColor].CGColor;
layer.bounds=CGRectMake(0, 0, 100, 100);
//添加圖層
[self.view.layer addSublayer:layer];
}
@end
顯示效果:
(1)設(shè)置錨點(diǎn)位置為(0,0)
- (void)viewDidLoad
{
[super viewDidLoad];
//創(chuàng)建圖層
CALayer *layer=[CALayer layer];
//設(shè)置圖層的屬性
layer.backgroundColor=[UIColor redColor].CGColor;
layer.bounds=CGRectMake(0, 0, 100, 100);
//設(shè)置錨點(diǎn)為(0,0)
layer.anchorPoint=CGPointZero;
//添加圖層
[self.view.layer addSublayer:layer];
}
@end
顯示效果:
二、隱式動(dòng)畫
1.簡單說明
每一個(gè)UIView內(nèi)部都默認(rèn)關(guān)聯(lián)著一個(gè)CALayer,我們可用稱這個(gè)Layer為Root Layer(根層)
所有的非Root Layer,也就是手動(dòng)創(chuàng)建的CALayer對(duì)象,都存在著隱式動(dòng)畫
什么是隱式動(dòng)畫?
當(dāng)對(duì)非Root Layer的部分屬性進(jìn)行修改時(shí),默認(rèn)會(huì)自動(dòng)產(chǎn)生一些動(dòng)畫效果
而這些屬性稱為Animatable Properties(可動(dòng)畫屬性)
列舉幾個(gè)常見的Animatable Properties:
bounds:用于設(shè)置CALayer的寬度和高度。修改這個(gè)屬性會(huì)產(chǎn)生縮放動(dòng)畫
backgroundColor:用于設(shè)置CALayer的背景色。修改這個(gè)屬性會(huì)產(chǎn)生背景色的漸變動(dòng)畫
position:用于設(shè)置CALayer的位置。修改這個(gè)屬性會(huì)產(chǎn)生平移動(dòng)畫
2.代碼示例
//
// YYViewController.m
// 04-隱式動(dòng)畫
//
// Created by apple on 14-6-21.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import "YYViewController.h"
@interface YYViewController ()
@property(nonatomic,strong)CALayer *layer;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//創(chuàng)建圖層
CALayer *mylayer=[CALayer layer];
//設(shè)置圖層屬性
mylayer.backgroundColor=[UIColor brownColor].CGColor;
mylayer.bounds=CGRectMake(0, 0, 150, 100);
//顯示位置
mylayer.position=CGPointMake(100, 100);
mylayer.anchorPoint=CGPointZero;
mylayer.cornerRadius=20;
//添加圖層
[self.view.layer addSublayer:mylayer];
self.layer=mylayer;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//隱式動(dòng)畫
self.layer.bounds=CGRectMake(0, 0, 200, 60);
self.layer.backgroundColor=[UIColor yellowColor].CGColor;
}
@end
關(guān)閉隱式動(dòng)畫:
[CATransaction begin];
[CATransaction setDisableActions:YES];
//隱式動(dòng)畫
self.layer.bounds=CGRectMake(0, 0, 200, 60);
self.layer.backgroundColor=[UIColor yellowColor].CGColor;
[CATransaction commit];
如何查看CALayer的某個(gè)屬性是否支持隱式動(dòng)畫?
可以查看頭文件,看有沒有Animatable,如果有則表示支持。
也可以查看官方文檔
文檔中標(biāo)明的這些屬性都是支持隱式動(dòng)畫的
自定義layer
一、第一種方式
1.簡單說明
以前想要在view中畫東西,需要自定義view,創(chuàng)建一個(gè)類與之關(guān)聯(lián),讓這個(gè)類繼承自UIView,然后重寫它的DrawRect:方法,然后在該方法中畫圖。
繪制圖形的步驟:
(1)獲取上下文
(2)繪制圖形
(3)渲染圖形
如果在layer上畫東西,與上面的過程類似。
代碼示例:
新建一個(gè)類,讓該類繼承自CALayer
YYMylayer.m文件
//
// YYMylayer.m
// 05-自定義layer(1)
//
// Created by apple on 14-6-21.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import "YYMylayer.h"
@implementation YYMylayer
//重寫該方法,在該方法內(nèi)繪制圖形
-(void)drawInContext:(CGContextRef)ctx
{
//1.繪制圖形
//畫一個(gè)圓
CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
//設(shè)置屬性(顏色)
// [[UIColor yellowColor]set];
CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
//2.渲染
CGContextFillPath(ctx);
}
@end
在控制器中,創(chuàng)建一個(gè)自定義的類
//
// YYViewController.m
// 05-自定義layer(1)
//
// Created by apple on 14-6-21.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import "YYViewController.h"
#import "YYMylayer.h"
@interface YYViewController ()
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//1.創(chuàng)建自定義的layer
YYMylayer *layer=[YYMylayer layer];
//2.設(shè)置layer的屬性
layer.backgroundColor=[UIColor brownColor].CGColor;
layer.bounds=CGRectMake(0, 0, 200, 150);
layer.anchorPoint=CGPointZero;
layer.position=CGPointMake(100, 100);
layer.cornerRadius=20;
layer.shadowColor=[UIColor blackColor].CGColor;
layer.shadowOffset=CGSizeMake(10, 20);
layer.shadowOpacity=0.6;
[layer setNeedsDisplay];
//3.添加layer
[self.view.layer addSublayer:layer];
}
@end
注意點(diǎn):
(1)默認(rèn)為無色,不會(huì)顯示。要想讓繪制的圖形顯示出來,還需要設(shè)置圖形的顏色。注意不能直接使用UI框架中的類
(2)在自定義layer中的-(void)drawInContext:方法不會(huì)自己調(diào)用,只能自己通過setNeedDisplay方法調(diào)用,在view中畫東西DrawRect:方法在view第一次顯示的時(shí)候會(huì)自動(dòng)調(diào)用。
實(shí)現(xiàn)效果:
2.拓展
UIView中繪圖說明
#import "YYVIEW.h"
@implementation YYVIEW
- (void)drawRect:(CGRect)rect
{
//1.獲取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.繪制圖形
CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
//設(shè)置屬性(顏色)
// [[UIColor yellowColor]set];
CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
//3.渲染
CGContextFillPath(ctx);
//在執(zhí)行渲染操作的時(shí)候,本質(zhì)上它的內(nèi)部相當(dāng)于調(diào)用了下面的方法
[self.layer drawInContext:ctx];
}
說明:在UIView中繪制圖形,獲取的上下文就是這個(gè)view對(duì)應(yīng)的layer的上下文。在渲染的時(shí)候,就是把圖形渲染到對(duì)應(yīng)的layer上。
在執(zhí)行渲染操作的時(shí)候,本質(zhì)上它的內(nèi)部相當(dāng)于執(zhí)行了 [self.layer drawInContext:ctx];
二、第二種方式
方法描述:設(shè)置CALayer的delegate,然后讓delegate實(shí)現(xiàn)drawLayer:inContext:方法,當(dāng)CALayer需要繪圖時(shí),會(huì)調(diào)用delegate的drawLayer:inContext:方法進(jìn)行繪圖。
代碼示例:
//
// YYViewController.m
// 06-自定義layer(2)
//
// Created by apple on 14-6-21.
// Copyright (c) 2014年 itcase. All rights reserved.
#import "YYViewController.h"
@interface YYViewController ()
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//1.創(chuàng)建自定義的layer
CALayer *layer=[CALayer layer];
//2.設(shè)置layer的屬性
layer.backgroundColor=[UIColor brownColor].CGColor;
layer.bounds=CGRectMake(0, 0, 200, 150);
layer.anchorPoint=CGPointZero;
layer.position=CGPointMake(100, 100);
layer.cornerRadius=20;
layer.shadowColor=[UIColor blackColor].CGColor;
layer.shadowOffset=CGSizeMake(10, 20);
layer.shadowOpacity=0.6;
//設(shè)置代理
layer.delegate=self;
[layer setNeedsDisplay];
//3.添加layer
[self.view.layer addSublayer:layer];
}
-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
//1.繪制圖形
//畫一個(gè)圓
CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
//設(shè)置屬性(顏色)
// [[UIColor yellowColor]set];
CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
//2.渲染
CGContextFillPath(ctx);
}
@end
實(shí)現(xiàn)效果:
注意點(diǎn):不能再將某個(gè)UIView設(shè)置為CALayer的delegate,因?yàn)閁IView對(duì)象已經(jīng)是它內(nèi)部根層的delegate,再次設(shè)置為其他層的delegate就會(huì)出問題。
在設(shè)置代理的時(shí)候,它并不要求我們遵守協(xié)議,說明這個(gè)方法是nsobject中的,就不需要再額外的顯示遵守協(xié)議了。
提示:以后如果要設(shè)置某個(gè)類的代理,但是這個(gè)代理沒要求我們遵守什么特定的協(xié)議,那么可以認(rèn)為這個(gè)協(xié)議方法是NSObject里邊的。
三、補(bǔ)充說明
(1)無論采取哪種方法來自定義層,都必須調(diào)用CALayer的setNeedsDisplay方法才能正常繪圖。
(2)詳細(xì)現(xiàn)實(shí)過程:
當(dāng)UIView需要顯示時(shí),它內(nèi)部的層會(huì)準(zhǔn)備好一個(gè)CGContextRef(圖形上下文),然后調(diào)用delegate(這里就是UIView)的drawLayer:inContext:方法,并且傳入已經(jīng)準(zhǔn)備好的CGContextRef對(duì)象。而UIView在drawLayer:inContext:方法中又會(huì)調(diào)用自己的drawRect:方法。平時(shí)在drawRect:中通過UIGraphicsGetCurrentContext()獲取的就是由層傳入的CGContextRef對(duì)象,在drawRect:中完成的所有繪圖都會(huì)填入層的CGContextRef中,然后被拷貝至屏幕。
相關(guān)文章
Flutter使用push pop方法及路由進(jìn)行導(dǎo)航詳解
這篇文章主要為大家介紹了Flutter使用push pop方法及路由進(jìn)行導(dǎo)航詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09iOS利用AFNetworking3.0——實(shí)現(xiàn)文件斷點(diǎn)下載
這篇文章主要介紹了iOS利用AFNetworking3.0——實(shí)現(xiàn)文件斷點(diǎn)下載,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01iOS實(shí)現(xiàn)無感知上拉加載更多功能的思路與方法
下拉刷新和上拉加載更多功能是一個(gè)應(yīng)用非常廣泛的一個(gè)效果,最新項(xiàng)目中就遇到這個(gè)功能,這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)無感知上拉加載更多功能的思路與方法,需要的朋友可以參考下2021-07-07UITableView 實(shí)現(xiàn)汽車品牌(demo)
UITableView堪稱UIKit里面最復(fù)雜的一個(gè)控件了,使用起來不算難,但是要用好并不容易,當(dāng)使用的時(shí)候我們必須要考慮到后臺(tái)數(shù)據(jù)的設(shè)計(jì),tableViewCell的設(shè)計(jì)和重用以及tableView的效率等問題,下面小編通過UITableView 實(shí)現(xiàn)汽車品牌,需要的朋友可以參考下2015-08-08iOS視頻壓縮存儲(chǔ)至本地并上傳至服務(wù)器實(shí)例代碼
本篇文章主要介紹了iOS視頻壓縮存儲(chǔ)至本地并上傳至服務(wù)器實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04