实现顶部cell和底部cell自定义部分圆角和投影的功能

2020-04-22  本文已影响0人  CocoaJason
Simulator Screen Shot - iPhone 11 Pro - 2020-04-22 at 18.02.05.png
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [(HZTableViewCell *)cell handleWillDisplayCellWithIndexPath:indexPath numberOfRows:[tableView numberOfRowsInSection:indexPath.section]];

}
//
//  HZTableViewCell.m
//  CollectionView
//
//  Created by 黄震 on 2020/3/27.
//  Copyright © 2020 黄震. All rights reserved.
//

#import "HZTableViewCell.h"


@interface HZTableViewCell ()


@end

@implementation HZTableViewCell


- (void)handleWillDisplayCellWithIndexPath:(NSIndexPath *)indexPath
                              numberOfRows:(NSInteger)count;{
    
    CGRect bounds = CGRectInset(self.bounds, 10, - 1);
    
    /*
     为了切割掉多余的投影
     将顶部和底部的backgroundView进行一些偏移,并且打开裁剪
     */
    if (count > 1) {
        if (indexPath.row == 0) {
            bounds = CGRectMake(10, 3, self.bounds.size.width - 20, self.bounds.size.height + 3);
        } else if (indexPath.row == count - 1) {
            bounds = CGRectMake(10, -5, self.bounds.size.width - 20, self.bounds.size.height + 3);
        }
    }
    UIView *backgroundView = [[UIView alloc] initWithFrame:bounds];
    if (count > 1) {
        backgroundView.clipsToBounds = YES;
    }
    self.backgroundView = backgroundView;
    
    /*
     将背景色都设置为透明色,以方便自定义
     */
    backgroundView.backgroundColor =
    self.backgroundColor =
    self.contentView.backgroundColor = UIColor.clearColor;
    
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    layer.shadowColor = [UIColor cyanColor].CGColor;
    layer.shadowOffset = CGSizeZero;
    layer.shadowOpacity = 0.5;
    layer.fillColor = [UIColor whiteColor].CGColor;
    
    UIBezierPath*path = [UIBezierPath bezierPathWithRect:bounds];
    CGSize cornerRadii = CGSizeZero;
    UIRectCorner corners = UIRectCornerAllCorners;
    
    /*
     1.当数量大于1时
     1.1 第一行实现顶部圆角,最后一行实现底部圆角,中间部分不加圆角
     1.2 当不是第一行的时候,添加分割线
     2.当数量为1时(只有一行)
     2.1实现全圆角
     */
    if (count > 1) {
        if (indexPath.row == 0) {
            cornerRadii = CGSizeMake(10, 10);
            corners = UIRectCornerTopLeft  | UIRectCornerTopRight;
        } else if(indexPath.row == count - 1) {
            cornerRadii = CGSizeMake(10, 10);
            corners = UIRectCornerBottomLeft  | UIRectCornerBottomRight;
        }
        if (corners != UIRectCornerAllCorners) {
            path = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:corners cornerRadii:cornerRadii];
        } else {
            path = [UIBezierPath bezierPathWithRect:bounds];
        }
        
        if (indexPath.row != 0) {
            CALayer *border=[[CALayer alloc] init];
            border.frame = CGRectMake(CGRectGetMinX(bounds),0, CGRectGetWidth(bounds), 1/[UIScreen mainScreen].scale);
            border.backgroundColor=[UIColor separatorColor].CGColor;
            [layer addSublayer:border];
        }
    }else {
        cornerRadii = CGSizeMake(10, 10);
        path =[UIBezierPath bezierPathWithRoundedRect:bounds
                                    byRoundingCorners:UIRectCornerAllCorners
                                          cornerRadii:cornerRadii];
    }
    layer.path = path.CGPath;
    [backgroundView.layer insertSublayer:layer atIndex:0];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end

上一篇下一篇

猜你喜欢

热点阅读