UIButton
//加陰影--任海麗編輯
//shadowColor陰影顏色
_imageView.layer.shadowColor = [UIColor blackColor].CGColor;
//shadowOffset陰影偏移,x向右偏移4,y向下偏移4,默認(rèn)(0, -3),這個(gè)跟shadowRadius配合使用
_imageView.layer.shadowOffset = CGSizeMake(4,4);
//陰影透明度,默認(rèn)0
_imageView.layer.shadowOpacity = 0.8;
//陰影半徑,默認(rèn)3
_imageView.layer.shadowRadius = 4;
UITableView
_tableView.layer.shadowOffset = CGSizeMake(0, 0);
_tableView.layer.shadowColor = [UIColor blackColor].CGColor;
_tableView.layer.shadowOpacity = 0.8;
_tableView.separatorColor = [UIColor colorWithWhite:0.3 alpha:1];
_tableView.clipsToBounds = NO;
clipsToBounds
是指視圖上的子視圖,如果超出父視圖的部分就截取掉,
masksToBounds
卻是指視圖的圖層上的子圖層,如果超出父圖層的部分就截取掉
然而,這種最簡單的添加陰影的方法在性能上卻不是最佳途徑。如果對這個(gè)添加陰影的View(如果它是一個(gè)UITableViewCell的一部分)做一些動畫,您可能會注意到在動畫不是很流暢,有卡頓。這是因?yàn)橛?jì)算陰影需要Core Animation做一個(gè)離屏渲染,以View準(zhǔn)確的形狀確定清楚如何呈現(xiàn)其陰影。
只要你提前告訴CoreAnimation你要渲染的View的形狀Shape,就會減少離屏渲染計(jì)算
[myView.layer setShadowPath:[[UIBezierPath bezierPathWithRect:myView.bounds] CGPath];
加上這行代碼,就減少離屏渲染時(shí)間,大大提高了性能