Files
mier_ios/SweetParty/Expand/Category/UITableViewCell+CSCellShadows.m

183 lines
8.4 KiB
Mathematica
Raw Permalink Normal View History

2025-08-11 10:43:19 +08:00
//
// UITableViewCell+CSCellShadows.m
// CoinShang
//
// Created by Jiayu_iOSer on 2018/11/27.
// Copyright © 2018 Shangbee. All rights reserved.
//
#import "UITableViewCell+CSCellShadows.h"
@implementation UITableViewCell (CSCellShadows)
- (void)addShadowToCellInTableView:(UITableView *)tableView
atIndexPath:(NSIndexPath *)indexPath
{
// section
BOOL isFirstRow = !indexPath.row;// cell
BOOL isLastRow = (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1);//cell
BOOL isSectionOneRow = ([tableView numberOfRowsInSection:indexPath.section] == 1);// cell
// the shadow rect determines the area in which the shadow gets drawn
CGRect shadowRect = CGRectInset(CGRectMake(0, 0, ScreenWidth, self.frame.size.height), 10, -10);
if(isFirstRow)
shadowRect.origin.y += 10;
else if(isLastRow)
shadowRect.size.height -= 10;
// the mask rect ensures that the shadow doesn't bleed into other table cells
CGRect maskRect = CGRectInset(CGRectMake(0, 0, ScreenWidth, self.frame.size.height), -10, 0);
if(isFirstRow) {
maskRect.origin.y -= 10;
maskRect.size.height += 10;
}
else if(isLastRow)
maskRect.size.height += 10;
// section cell
if (isSectionOneRow) {
for (int i = 0; i < 2; i++) {
shadowRect.size.height -= 10;
maskRect.size.height += 5;
}
}
// now configure the background view layer with the shadow
CALayer *layers = self.backgroundView.layer;
layers.shadowColor = [UIColor lightGrayColor].CGColor;
layers.shadowOffset = CGSizeMake(0, 0);
layers.shadowRadius = 2.5;
layers.shadowOpacity = 0.2f;
layers.shadowPath = [UIBezierPath bezierPathWithRoundedRect:shadowRect cornerRadius:8].CGPath;
layers.masksToBounds = NO;
// and finally add the shadow mask
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRect:maskRect].CGPath;
layers.mask = maskLayer;
/*
BOOL isFirstRow = !indexPath.row;
BOOL isLastRow = (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1);
// the shadow rect determines the area in which the shadow gets drawn
CGRect shadowRect = CGRectInset(self.backgroundView.bounds, 0, -10);
if(isFirstRow)
shadowRect.origin.y += 10;
else if(isLastRow)
shadowRect.size.height -= 10;
// the mask rect ensures that the shadow doesn't bleed into other table cells
CGRect maskRect = CGRectInset(self.backgroundView.bounds, -20, 0);
if(isFirstRow) {
maskRect.origin.y -= 10;
maskRect.size.height += 10;
}
else if(isLastRow)
maskRect.size.height += 10;
// now configure the background view layer with the shadow
CALayer *layer = self.backgroundView.layer;
layer.shadowColor = [UIColor redColor].CGColor;
layer.shadowOffset = CGSizeMake(0, 0);
layer.shadowRadius = 3;
layer.shadowOpacity = 0.75;
layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:shadowRect cornerRadius:5].CGPath;
layer.masksToBounds = NO;
// and finally add the shadow mask
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRect:maskRect].CGPath;
layer.mask = maskLayer;
*/
}
/*
/ tableview section
/
*/
- (void)addShadowToSectionTableView:(UITableView *)tableView
atIndexPath:(NSIndexPath *)indexPath{
//
CGFloat cornerRadius = 7.f;
// cell
self.backgroundColor = UIColor.clearColor;
// shapeLayer
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
CAShapeLayer *backgroundLayer = [[CAShapeLayer alloc] init]; //
// Path
CGMutablePathRef pathRef = CGPathCreateMutable();
// cellsize
// , cell bounds, ,
CGRect bounds = CGRectInset(self.bounds, 10, 0);
// CGRectGetMinY
// CGRectGetMaxY
// CGRectGetMinX
// CGRectGetMaxX
// CGRectGetMidX: X
// CGRectGetMidY: Y
// sectionsection
// CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
// pCGRectGetMinX(bounds), CGRectGetMinY(bounds)p1(x1,y1)(CGRectGetMidX(bounds), CGRectGetMinY(bounds))p2(x2,y2)p1p2线l1pp1线l线r
if (indexPath.row == 0) {
if ([tableView numberOfRowsInSection:indexPath.section] == 1) {
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);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMinX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
}else{
// cell
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));
}
} else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
// cell
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 {
// cellrectanglepath
CGPathAddRect(pathRef, nil, bounds);
}
// pathrender
layer.path = pathRef;
backgroundLayer.path = pathRef;
// Quartz2Dcreat/copy/retain
CFRelease(pathRef);
// shape layerpathrender
// layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;
layer.fillColor = [UIColor whiteColor].CGColor;
// viewcell
UIView *roundView = [[UIView alloc] initWithFrame:bounds];
// roundView
[roundView.layer insertSublayer:layer atIndex:0];
roundView.backgroundColor = UIColor.clearColor;
// cellview
self.backgroundView = roundView;
}
@end