UITableViewCell多分組下部分圓角
實現效果如圖:
直接上demo::https://github.com/mrzhao12/UITableViewCell-
https://juejin.cn/post/7277362508813500457
.h文件:
#import
NS_ASSUME_NONNULL_BEGIN
@interface LLJSUIKitHelper : NSObject
-(void)LLJCView:(UIView*)viewcornerRadius:(NSArray*)cornerRadius;
@end
NS_ASSUME_NONNULL_END
.m文件
#import "LLJSUIKitHelper.h"
@implementation LLJSUIKitHelper
-(void)LLJCView:(UIView*)subViewcornerRadius:(NSArray*)cornerRadius{
? ? UIBezierPath *path = [[UIBezierPath alloc] init];
? ? NSMutableArray*cornerArray? = [NSMutableArray array];
? ? for(inti =0; i
? ? ? ? if(i ==0){
? ? ? ? ? ? [cornerArrayaddObject:@(UIRectCornerTopLeft)];
? ? ? ? }elseif(i ==1){
? ? ? ? ? ? [cornerArrayaddObject:@(UIRectCornerTopRight)];
? ? ? ? }elseif(i ==2){
? ? ? ? ? ? [cornerArrayaddObject:@(UIRectCornerBottomRight)];
? ? ? ? }elseif(i ==3){
? ? ? ? ? ? [cornerArrayaddObject:@(UIRectCornerBottomLeft)];
? ? ? ? }
? ? }
? ? path = [selfdrawRoundedRect:subView.boundsbyRoundingCorners:cornerArraycornerRadius:cornerRadius];
? ? CAShapeLayer *subLayer = [CAShapeLayer layer];
? ? subLayer.fillColor = [UIColor whiteColor].CGColor;
? ? subLayer.path= path.CGPath;
? ? subLayer.frame= subView.bounds;
? ? subView.layer.mask= subLayer;
}
-(UIBezierPath*)drawRoundedRect:(CGRect)rectbyRoundingCorners:(NSMutableArray*)byRoundingCornerscornerRadius:(NSArray*)cornerRadius{
? ? UIBezierPath *path = [[UIBezierPath alloc] init];
? ? CGPoint? startPoint =CGPointMake(rect.origin.x, rect.origin.y);
? ? //處理第一個角
? ? if ([byRoundingCorners containsObject:@(UIRectCornerTopLeft)]) {
? ? ? ? NSNumber*position = cornerRadius.firstObject;// ------>cornerRadius
? ? ? ? CGFloatmycg = position.floatValue;
? ? ? ? [pathmoveToPoint:CGPointMake(startPoint.x+ mycg, startPoint.y)];
? ? }else{
? ? ? ? [pathmoveToPoint:CGPointMake(startPoint.x, startPoint.y)];
? ? }
? ? //處理第二個角
? ? if ([byRoundingCorners containsObject:@(UIRectCornerTopRight)]) {
? ? ? ? NSNumber*position = [cornerRadiusobjectAtIndex:1];
? ? ? ? CGFloatmycg = position.floatValue;
? ? ? ? [pathaddLineToPoint:CGPointMake(startPoint.x+ rect.size.width- mycg, startPoint.y)];
? ? ? ? [pathaddArcWithCenter:CGPointMake(startPoint.x+ rect.size.width- mycg, startPoint.y+ mycg)radius:mycgstartAngle: (M_PI*3/2)endAngle:0clockwise:YES];
? ? }else{
? ? ? ? [pathaddLineToPoint:CGPointMake(startPoint.x+ rect.size.width, startPoint.y)];
? ? }
? ? //處理第三個角
? ? if ([byRoundingCorners containsObject:@(UIRectCornerBottomRight)]) {
? ? ? ? NSNumber*position = [cornerRadiusobjectAtIndex:2];
? ? ? ? CGFloatmycg = position.floatValue;
? ? ? ? [pathaddLineToPoint:CGPointMake(startPoint.x+ rect.size.width, startPoint.y+rect.size.height)];
? ? ? ? [pathaddArcWithCenter:CGPointMake(startPoint.x+rect.size.width-mycg, startPoint.y+rect.size.height-mycg)radius:mycgstartAngle:0endAngle: (M_PI/2)clockwise:YES];
? ? }else{
? ? ? ? [pathaddLineToPoint:CGPointMake(startPoint.x+ rect.size.width, startPoint.y+ rect.size.height)];
? ? }
//? ? //處理第四個角
? ? if ([byRoundingCorners containsObject:@(UIRectCornerBottomLeft)]) {
? ? ? ? NSNumber*position = [cornerRadiusobjectAtIndex:3];
? ? ? ? CGFloatmycg = position.floatValue;
? ? ? ? [pathaddLineToPoint:CGPointMake(startPoint.x+ mycg, startPoint.y+rect.size.height)];
? ? ? ? [pathaddArcWithCenter:CGPointMake(startPoint.x+mycg, startPoint.y+ rect.size.height-mycg)radius:mycgstartAngle: (M_PI/2)endAngle: (M_PI)clockwise:YES];
? ? }else{
? ? ? ? [pathaddLineToPoint:CGPointMake(startPoint.x, startPoint.y+rect.size.height)];
? ? }
//? ? //再次處理第一個角
? ? if ([byRoundingCorners containsObject:@(UIRectCornerTopLeft)]) {
? ? ? ? NSNumber*position = cornerRadius.firstObject;
?? ? ? ? CGFloatmycg = position.floatValue;
? ? ? ? [pathaddLineToPoint:CGPointMake(startPoint.x, startPoint.y+ mycg)];
? ? ? ? [pathaddArcWithCenter:CGPointMake(startPoint.x+mycg, startPoint.y+ mycg)radius:mycgstartAngle: (M_PI)endAngle:(M_PI*3/2)clockwise:YES];
? ? }else{
? ? ? ? [pathaddLineToPoint:CGPointMake(startPoint.x, startPoint.y)];
? ? }
? ? returnpath;
}
@end
調用:直接在對應的TableView所在vc里寫如下即可:
- (void)tableView:(UITableView*)tableViewwillDisplayCell:(UITableViewCell*)cellforRowAtIndexPath:(NSIndexPath*)indexPath{
? ? //組切圓角
? ? //方法一 判斷第一個cell, 切出左上和又上圓角, 最后一個cell, 切出左下和右下圓角
? ? //方法二 使用帶圓角的圖片模擬
? ? LLJSUIKitHelper*kitHelper =? [[LLJSUIKitHelper alloc] init];
? ? if(indexPath.section==sectionSystem){
? ? ? ? [kitHelperLLJCView:cellcornerRadius:@[@(20),@(20),@(20),@(20)]];
? }? elseif(indexPath.section==sectionCommunity){
? ? ? [kitHelperLLJCView:cellcornerRadius:@[@(20),@(20),@(12),@(12)]];
? }
? elseif(indexPath.section==sectionWangyi){
? ? ? [kitHelperLLJCView:cellcornerRadius:@[@(20),@(10),@(40),@(20)]];
? }elseif(indexPath.section==sectionChat){
? ? ? if(self.recentSessions.count>0) {
? ? ? ? ? if(self.recentSessions.count==1) {
? ? ? ? ? ? ? [kitHelperLLJCView:cellcornerRadius:@[@(10),@(10),@(10),@(10)]];
? ? ? ? ? }else{
? ? ? ? ? ? ? if( indexPath.row==0) {
? ? ? ? ? ? ? ? ? [kitHelperLLJCView:cellcornerRadius:@[@(10),@(10),@(0),@(0)]];
? ? ? ? ? ? ? }elseif(indexPath.row==self.recentSessions.count-1) {
//? ? ? ? ? ? ? ? ? LLJSUIKitHelper.LLJCView(subView: cell, cornerRadius: [0,0,12,12])
? ? ? ? ? ? ? ? ? [kitHelperLLJCView:cellcornerRadius:@[@(0),@(0),@(10),@(10)]];
? ? ? ? ? ? ? }
? ? ? ? ? }
? ? ? }
? }
? elseif(indexPath.section==sectionGray){
? ? ? [kitHelperLLJCView:cellcornerRadius:@[@(20),@(10),@(40),@(20)]];
? }
}
----------------
swift代碼如下:
importUIKit
public class LLJSUIKitHelper {
? ? //UIView
? ? class func LLJView(backGroundColor: UIColor?, frame: CGRect?) -> UIView {
? ? ? ? letview =UIView()
? ? ? ? if(backGroundColor !=nil) {
? ? ? ? ? ? view.backgroundColor= backGroundColor
? ? ? ? }
? ? ? ? if(frame !=nil) {
? ? ? ? ? ? view.frame= frame!
? ? ? ? }
? ? ? ? returnview
? ? }
? ? //UIView 切統(tǒng)一圓角
? ? class func LLJCView(subView: UIView, cornerRadius: CGFloat) {
? ? ? ? // 圓角
? ? ? ? LLJCView(subView: subView,cornerRadius: [cornerRadius,cornerRadius,cornerRadius,cornerRadius])
? ? }
? ? //UIView 切指定不同圓角 cornerRadius = [8,10,12,14] 一次對應topLeft,topRight,bottomRight,bottomLeft的圓角半徑
? ? class func LLJCView(subView: UIView, cornerRadius: [CGFloat]) {
? ? ? ? // 圓角
? ? ? ? varpath:UIBezierPath?
? ? ? ? varcornerArray = [UIRectCorner]()
? ? ? ? foriinstride(from:0,to: cornerRadius.count,by:1) {
? ? ? ? ? ? ifi ==0{
? ? ? ? ? ? ? ? cornerArray.append(UIRectCorner.topLeft)
? ? ? ? ? ? }elseifi ==1{
? ? ? ? ? ? ? ? cornerArray.append(UIRectCorner.topRight)
? ? ? ? ? ? }elseifi ==2{
? ? ? ? ? ? ? ? cornerArray.append(UIRectCorner.bottomRight)
? ? ? ? ? ? }elseifi ==3{
? ? ? ? ? ? ? ? cornerArray.append(UIRectCorner.bottomLeft)
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? path =LLJBezierPath.drawRoundedRect(rect: subView.bounds,byRoundingCorners: cornerArray,cornerRadius: cornerRadius)
? ? ? ? letsubLayer =CAShapeLayer()
? ? ? ? subLayer.path= path!.cgPath
? ? ? ? subLayer.frame= subView.bounds
? ? ? ? subView.layer.mask= subLayer;
? ? }
? ? //按鈕
? ? class func LLJButton(title: String?, titleColor: UIColor?, backGroundColor: UIColor?, titleFont: UIFont?, frame: CGRect?) -> UIButton {
? ? ? ? let button = UIButton(type: UIButton.ButtonType.custom)
? ? ? ? if(title !=nil) {
? ? ? ? ? ? button.setTitle(title,for:UIControl.State.normal)
? ? ? ? }
? ? ? ? if(titleColor !=nil) {
? ? ? ? ? ? button.setTitleColor(titleColor,for:UIControl.State.normal)
? ? ? ? }
? ? ? ? if(backGroundColor !=nil) {
? ? ? ? ? ? button.backgroundColor= backGroundColor
? ? ? ? }
? ? ? ? if(titleFont !=nil) {
? ? ? ? ? ? button.titleLabel?.font= titleFont
? ? ? ? }
? ? ? ? if(frame !=nil) {
? ? ? ? ? ? button.frame= frame!
? ? ? ? }
? ? ? ? returnbutton;
? ? }
? ? //Label
? ? class func LLJLabel(title: String?, titleColor: UIColor?, backGroundColor: UIColor?, titleFont: UIFont?, frame: CGRect?, numberOfLines: Int) -> UILabel {
? ? ? ? letlabel =UILabel()
? ? ? ? if(title !=nil) {
? ? ? ? ? ? label.text= title
? ? ? ? }
? ? ? ? if(titleColor !=nil) {
? ? ? ? ? ? label.textColor= titleColor
? ? ? ? }
? ? ? ? if(backGroundColor !=nil) {
? ? ? ? ? ? label.backgroundColor= backGroundColor
? ? ? ? }
? ? ? ? if(titleFont !=nil) {
? ? ? ? ? ? label.font= titleFont
? ? ? ? }
? ? ? ? if(frame !=nil) {
? ? ? ? ? ? label.frame= frame!
? ? ? ? }
? ? ? ? label.numberOfLines= numberOfLines
? ? ? ? returnlabel;
? ? }
}
importUIKit
class LLJBezierPath {
? ? /*
?? ? * 畫圓 橢圓
?? ? * rect.w = rect.h 時畫圓; rect.w != rect.h 畫橢圓
?? ? */
? ? class func drawCyle(rect: CGRect) -> UIBezierPath {
? ? ? ? letpath =UIBezierPath.init(ovalIn: rect)
? ? ? ? returnpath
? ? }
? ? /*
?? ? * 畫圓 圓弧
?? ? * arcCenter: 圓心
?? ? * radius: 半徑
?? ? * startAngle: 開始角度
?? ? * endAngle: 結束角度
?? ? * clockwise: 順時針 yes 逆時針 no
?? ? */
? ? class func drawCyle(arcCenter: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool) -> UIBezierPath {
? ? ? ? letpath =UIBezierPath.init(arcCenter: arcCenter,radius: radius,startAngle: startAngle,endAngle: endAngle,clockwise: clockwise)
? ? ? ? returnpath
? ? }
? ? /*
?? ? * 畫矩形 統(tǒng)一圓角
?? ? * rect: 矩形rect
?? ? */
? ? class func drawRoundedRect(rect: CGRect, cornerRadius: CGFloat?) -> UIBezierPath {
? ? ? ? letpath =UIBezierPath.init(roundedRect: rect,cornerRadius: cornerRadius ??0.0)
? ? ? ? returnpath
? ? }
? ? /*
?? ? * 畫矩形 單一圓角
?? ? * rect: 矩形rect
?? ? */
? ? class func drawRoundedRect(rect: CGRect, byRoundingCorners: UIRectCorner, cornerRadius: CGFloat) -> UIBezierPath {
? ? ? ? letpath =UIBezierPath.init(roundedRect: rect,byRoundingCorners: byRoundingCorners,cornerRadii:CGSize(width: cornerRadius,height: cornerRadius))
? ? ? ? returnpath
? ? }
? ? /*
?? ? * 畫矩形 可變圓角
?? ? * byRoundingCorners: 需要切的圓角
?? ? * cornerRadius: 圓角半徑
?? ? * 注意:byRoundingCorners要和cornerRadius一一對應琳轿。
?? ? * 如:[UIRectCorner.topLeft,UIRectCorner.topRight,UIRectCorner.bottomLeft,UIRectCorner.bottomRight] 對應 [10,5,5,10]
?? ? */
? ? class func drawRoundedRect(rect: CGRect, byRoundingCorners: [UIRectCorner], cornerRadius: [CGFloat]) -> UIBezierPath {
? ? ? ? letpath =UIBezierPath()
? ? ? ? letstartPoint =CGPoint(x: rect.origin.x,y: rect.origin.y)
? ? ? ? //處理第一個角
? ? ? ? ifbyRoundingCorners.contains(UIRectCorner.topLeft) {
? ? ? ? ? ? letposition = byRoundingCorners.firstIndex(of:UIRectCorner.topLeft)
? ? ? ? ? ? path.move(to:CGPoint(x: startPoint.x+ cornerRadius[position!],y: startPoint.y))
? ? ? ? }else{
? ? ? ? ? ? path.move(to:CGPoint(x: startPoint.x,y: startPoint.y))
? ? ? ? }
? ? ? ? //處理第二個角
? ? ? ? ifbyRoundingCorners.contains(UIRectCorner.topRight) {
? ? ? ? ? ? letposition = byRoundingCorners.firstIndex(of:UIRectCorner.topRight)
? ? ? ? ? ? path.addLine(to:CGPoint(x: startPoint.x+ rect.width- cornerRadius[position!],y: startPoint.y))
? ? ? ? ? ? path.addArc(withCenter:CGPoint(x: startPoint.x+ rect.width- cornerRadius[position!],y: startPoint.y+ cornerRadius[position!]),radius: cornerRadius[position!],startAngle:CGFloat.pi*3/2,endAngle:0,clockwise:true)
? ? ? ? }else{
? ? ? ? ? ? path.addLine(to:CGPoint(x: startPoint.x+ rect.width,y: startPoint.y))
? ? ? ? }
? ? ? ? //處理第三個角
? ? ? ? ifbyRoundingCorners.contains(UIRectCorner.bottomRight) {
? ? ? ? ? ? letposition = byRoundingCorners.firstIndex(of:UIRectCorner.bottomRight)
? ? ? ? ? ? path.addLine(to:CGPoint(x: startPoint.x+ rect.width,y: startPoint.y+ rect.height- cornerRadius[position!]))
? ? ? ? ? ? path.addArc(withCenter:CGPoint(x: startPoint.x+ rect.width- cornerRadius[position!],y: startPoint.y+ rect.height- cornerRadius[position!]),radius: cornerRadius[position!],startAngle:0,endAngle:CGFloat.pi/2,clockwise:true)
? ? ? ? }else{
? ? ? ? ? ? path.addLine(to:CGPoint(x: startPoint.x+ rect.width,y: startPoint.y+ rect.height))
? ? ? ? }
? ? ? ? //處理第四個角
? ? ? ? ifbyRoundingCorners.contains(UIRectCorner.bottomLeft) {
? ? ? ? ? ? letposition = byRoundingCorners.firstIndex(of:UIRectCorner.bottomLeft)
? ? ? ? ? ? path.addLine(to:CGPoint(x: startPoint.x+ cornerRadius[position!],y: startPoint.y+ rect.height))
? ? ? ? ? ? path.addArc(withCenter:CGPoint(x: startPoint.x+ cornerRadius[position!],y: startPoint.y+ rect.height- cornerRadius[position!]),radius: cornerRadius[position!],startAngle:CGFloat.pi/2,endAngle:CGFloat.pi,clockwise:true)
? ? ? ? }else{
? ? ? ? ? ? path.addLine(to:CGPoint(x: startPoint.x,y: startPoint.y+ rect.height))
? ? ? ? }
? ? ? ? //再次處理第一個角
? ? ? ? ifbyRoundingCorners.contains(UIRectCorner.topLeft) {
? ? ? ? ? ? letposition = byRoundingCorners.firstIndex(of:UIRectCorner.topLeft)
? ? ? ? ? ? path.addLine(to:CGPoint(x: startPoint.x,y: startPoint.y+ cornerRadius[position!]))
? ? ? ? ? ? path.addArc(withCenter:CGPoint(x: startPoint.x+ cornerRadius[position!],y: startPoint.y+ cornerRadius[position!]),radius: cornerRadius[position!],startAngle:CGFloat.pi,endAngle:CGFloat.pi*3/2,clockwise:true)
? ? ? ? }else{
? ? ? ? ? ? path.addLine(to:CGPoint(x: startPoint.x,y: startPoint.y))
? ? ? ? }
? ? ? ? returnpath
? ? }
? ? /*
?? ? * 畫多邊行 或折線
?? ? * pointArray:多邊形頂點數組脸候,第一個點默認起點
?? ? * closePath:true表示關閉莫矗,即多邊形烛占;false表示不關閉,即折線圖
?? ? */
? ? class func drawPolygon(pointArray: Array<CGPoint>, closePath: Bool) -> UIBezierPath {
? ? ? ? letpath =UIBezierPath()
? ? ? ? foriinstride(from:0,to: pointArray.count,by:1) {
? ? ? ? ? ? letpoint = pointArray[i]
? ? ? ? ? ? ifi ==0{
? ? ? ? ? ? ? ? path.move(to: point)
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? path.addLine(to: point)
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? ifclosePath {
? ? ? ? ? ? path.close()
? ? ? ? }
? ? ? ? returnpath
? ? }
? ? /*
?? ? * 畫曲線 單曲線
?? ? * startPoint: 起點
?? ? * endPoint: 終點
?? ? * controlPoint: 控制點
?? ? */
? ? class func drawoQuadCurve(startPoint: CGPoint, endPoint: CGPoint, controlPoint: CGPoint) -> UIBezierPath {
? ? ? ? letpath =UIBezierPath()
? ? ? ? path.move(to: startPoint)
? ? ? ? path.addQuadCurve(to: endPoint,controlPoint: controlPoint)
? ? ? ? returnpath
? ? }
? ? /*
?? ? * 畫曲線 雙曲線
?? ? * startPoint: 起點
?? ? * endPoint: 終點
?? ? * controlPoint1: 控制點1
?? ? * controlPoint2: 控制點2
?? ? */
? ? class func drawCurve(startPoint: CGPoint, endPoint: CGPoint, controlPoint1: CGPoint, controlPoint2: CGPoint) -> UIBezierPath {
? ? ? ? letpath =UIBezierPath()
? ? ? ? path.move(to: startPoint)
? ? ? ? path.addCurve(to: endPoint,controlPoint1: controlPoint1,controlPoint2: controlPoint2)
? ? ? ? returnpath
? ? }
}
調用:
extension LLJWChatMainViewController: UITableViewDelegate {
? ??functableView(_tableView:UITableView,willDisplaycell:UITableViewCell,forRowAtindexPath:IndexPath) {
? ? ? ? //組切圓角
? ? ? ? //方法一 判斷第一個cell, 切出左上和又上圓角, 最后一個cell, 切出左下和右下圓角
? ? ? ? //方法二 使用帶圓角的圖片模擬
? ? ? ? letsubArray =self.dataArray[indexPath.section]
? ? ? ? //切圓角
? ? ? ? ifsubArray.count==1{
? ? ? ? ? ? LLJSUIKitHelper.LLJCView(subView: cell,cornerRadius: [12,12,12,12])
? ? ? ? }else{
? ? ? ? ? ? ifindexPath.row==0{
? ? ? ? ? ? ? ? LLJSUIKitHelper.LLJCView(subView: cell,cornerRadius: [12,12,0,0])
? ? ? ? ? ? }elseif(indexPath.row== subArray.count-1) {
? ? ? ? ? ? ? ? LLJSUIKitHelper.LLJCView(subView: cell,cornerRadius: [0,0,12,12])
? ? ? ? ? ? }
? ? ? ? }
? ? }
}