groupTest.png
如何实现分组列表(group tableview)的圆角效果?如上图:
主要实现思路
1.通过Core Graphics API来实现图层重绘
2.实现UITableViewDelegate协议中的willDisplayCell方法
具体思路
1.cell背景色设为透明
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;
[layeraddSublayer:lineLayer];
}
UIView*testView = [[UIViewalloc]initWithFrame:bounds];
[testView.layerinsertSublayer:layeratIndex:0];
testView.backgroundColor=UIColor.clearColor;
cell.backgroundView= testView;
} } }