groupTest.png
如何實(shí)現(xiàn)分組列表(group tableview)的圓角效果?如上圖:
主要實(shí)現(xiàn)思路
1.通過Core Graphics API來實(shí)現(xiàn)圖層重繪
2.實(shí)現(xiàn)UITableViewDelegate協(xié)議中的willDisplayCell方法
具體思路
1.cell背景色設(shè)為透明
2.新建圖層
3.圓角矩形圖層繪制
4.把該圖層作為自子圖層賦給UIView
5.UIView賦給cell的backgroundView
核心代碼
-?(void)tableView:(UITableView*)tableView?willDisplayCell:(UITableViewCell*)cell?forRowAtIndexPath:(NSIndexPath*)indexPath
{
if([cellrespondsToSelector:@selector
(tintColor)]) {
if(tableView ==self.tableView
) {
CGFloatcornerRadius =5.f
;
cell.
backgroundColor=UIColor.clearColor
;
CAShapeLayer*layer = [[CAShapeLayeralloc]init
];
CGMutablePathRefpathRef =CGPathCreateMutable
();
CGRectbounds =CGRectInset(cell.bounds,10,0
);
BOOLaddLine =NO
;
if(indexPath.row==0&& indexPath.row== [tableViewnumberOfRowsInSection:indexPath.section]-1
) {
CGPathAddRoundedRect(pathRef,nil
, bounds, cornerRadius, cornerRadius);
}
elseif(indexPath.row==0
) {
CGPathMoveToPoint(pathRef,nil,CGRectGetMinX(bounds),CGRectGetMaxY
(bounds));
CGPathAddArcToPoint(pathRef,nil,CGRectGetMinX(bounds),CGRectGetMinY(bounds),CGRectGetMidX(bounds),CGRectGetMinY
(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef,nil,CGRectGetMaxX(bounds),CGRectGetMinY(bounds),CGRectGetMaxX(bounds),CGRectGetMidY
(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef,nil,CGRectGetMaxX(bounds),CGRectGetMaxY
(bounds));
addLine =
YES
;
}
elseif(indexPath.row== [tableViewnumberOfRowsInSection:indexPath.section]-1
) {
CGPathMoveToPoint(pathRef,nil,CGRectGetMinX(bounds),CGRectGetMinY
(bounds));
CGPathAddArcToPoint(pathRef,nil,CGRectGetMinX(bounds),CGRectGetMaxY(bounds),CGRectGetMidX(bounds),CGRectGetMaxY
(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef,nil,CGRectGetMaxX(bounds),CGRectGetMaxY(bounds),CGRectGetMaxX(bounds),CGRectGetMidY
(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef,nil,CGRectGetMaxX(bounds),CGRectGetMinY
(bounds));
}
else
{
CGPathAddRect(pathRef,nil
, bounds);
addLine =
YES
;
}
layer.
path
= pathRef;
CFRelease
(pathRef);
layer.
fillColor= [UIColorcolorWithWhite:1.falpha:0.8f].CGColor
;
if(addLine ==YES
) {
CALayer*lineLayer = [[CALayeralloc]init
];
CGFloatlineHeight = (1.f/ [UIScreenmainScreen].scale
);
lineLayer.
frame=CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10
, lineHeight);
lineLayer.
backgroundColor= tableView.separatorColor.CGColor
;
[layer
addSublayer
:lineLayer];
}
UIView*testView = [[UIViewalloc]initWithFrame
:bounds];
[testView.
layerinsertSublayer:layeratIndex:0
];
testView.
backgroundColor=UIColor.clearColor
;
cell.backgroundView= testView;
} } }