CALayer 是一个很经常使用的到的 Object,很常用,也很重要,同时又有比较多的属性,嗯,一定要详细了解下。
CALayer 的原理很难懂,由于我目前只注重运用,所以就说那些在实际开发中能用到的一些地方。
如果要更改 UIView 的 CALayer,要引用:#import <QuartzCore/QuartzCore.h>.
下面先推荐两篇文章: 文章 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; }