CALayer 是一個很經常使用的到的 Object,很常用,也很重要,同時又有比較多的屬性,嗯,一定要詳細瞭解下
CALayer 的原理很難懂,由於我目前只注重運用,所以就說那些在實際開發中能用到的一些地方
下面先推薦兩篇文章: 文章 1:原理介紹的比較詳細; 2:官方文章
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *viewSample = [[UIView alloc] init];
[self.view addSubview:viewSample];
viewSample.backgroundColor = [UIColor greenColor];
viewSample.frame = CGRectMake(100, 100, 400, 400);
//Test 1 陰影
//viewSample.layer.shadowPath = [UIBezierPath bezierPathWithRect:viewSample.bounds].CGPath;
viewSample.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 400, 400)].CGPath;
viewSample.layer.masksToBounds = NO;
viewSample.layer.shadowOffset = CGSizeMake(10, 10);
viewSample.layer.shadowRadius = 5;
viewSample.layer.shadowOpacity = 0.5;
//Test 2 邊框
viewSample.layer.borderWidth = 2;
viewSample.layer.borderColor = [[UIColor redColor] CGColor];
//Test 3 masksToBounds
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0,0, 500, 500)];
btn.backgroundColor = [UIColor lightGrayColor];
//[viewSample addSubview:btn];
//viewSample.layer.masksToBounds = true;
//Test 4 bounds
//viewSample.layer.bounds = CGRectMake(200, 200, 500, 500);
//Test 5
viewSample.layer.opacity = 0.5;
viewSample.layer.cornerRadius = 5;
}