您的位置:首页 > 产品设计 > UI/UE

CALayer的常用属性(笔记与分享)

2015-09-21 18:51 441 查看
1..UIView简述

UIView是iOS系统界面最基本的元素,UIView之所以能够显示东西给我们看是因为它的内部有个图层即CALayer(也就是UIView的一个属性)。创建UIView对象时,UIView内部自动创建一个图层(CALayer对象),UIView通过layer这个属性访问图层;当UIView需要显示到屏幕上时,调用drawRect方法进行绘图,并且会将内容绘制到图层上,绘图完毕,系统将图层拷贝到屏幕上,这就完成了UIView的显示功能。

2.CALayer和UIView的区别与选择

CALayer负责视图中显示内容和动画,不需要交互时,性能更高

UIView负责监听和响应事件,本身不具备显示功能,需要交互时,只能选择UIView

3.代码演示

#import "ViewController.h"
#import "TestView.h"

@interface ViewController ()

@property (weak, nonatomic)  TestView *testView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

TestView *testView = [[TestView alloc] init];

self.testView = testView;

//    testView.backgroundColor = [UIColor orangeColor];
testView.layer.backgroundColor = [UIColor orangeColor].CGColor;

testView.frame = CGRectMake(100, 100, 180, 180);

NSLog(@"%@",NSStringFromCGPoint(testView.frame.origin));
NSLog(@"%@",NSStringFromCGPoint(testView.layer.position));

[self.view addSubview:testView];

//UIView上添加图片(设置layer的内容)
testView.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"haoyuexing"].CGImage);

#pragma mark - 利用 position 调整 view 的位置
//    //只能设置view的大小,不能设置view的位置
//    testView.layer.bounds = CGRectMake(3000, 3000, 180, 180);
//    NSLog(@"%@",NSStringFromCGPoint(testView.layer.bounds.origin));
//    //Postion(没有anchorPoint情况下)默认的位置为view的中点,和bounds一起使用才能确定view,设置bounds和position才能完成上面frame的功能
//    testView.layer.position =CGPointMake(190, 190);
//    NSLog(@"%@",NSStringFromCGPoint(testView.layer.position));
//
//边框宽度
testView.layer.borderWidth = 10;

//边框颜色
testView.layer.borderColor = [UIColor orangeColor].CGColor;

testView.layer.shadowColor = [UIColor brownColor].CGColor;
//阴影偏移,在x轴和y轴上延伸的方向,即shadow的大小
testView.layer.shadowOffset = CGSizeMake(20, 20);

//阴影透明度,默认为0,不显示阴影
//shadow的渐变距离,从外围开始,往里都渐变shadowRadius的距离(利用截图可以清晰看得渐变距离,根本不是网上所说的什么圆角半径)
testView.layer.shadowRadius = 2;

//layer的圆角矩形半径
testView.layer.cornerRadius = 90;

//裁剪(裁剪头像的时候一定要加)
testView.layer.masksToBounds = YES;

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios CALayer uiview