iOS每日一记之———————————————为Cell设置部分圆角 类似于ipad的设置界面效果

来源:互联网 发布:在淘宝上如何做代理 编辑:程序博客网 时间:2024/05/22 03:27

需求图是这样的


整个是个tableView 而且要求第一个cell和最后一个cell是部分圆角  有人会说这很简单啊 你设置第一个cell和最后一个cell的contentView部分圆角不就好了么  然而这样实现不了的、。。。。。 我在cell上面放了个白色的View 然后控制白色View的不同圆角情况依旧失败。。。。。想了半天实在不行加个图片好了 但是因为功能比较大 不能再添加无效图片了 所以只好作罢 。。。。 之后在stackOverFollow上面找到了答案 具体实现如下  用到了disPlayCell方法




- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger sectionCount = [tableView numberOfRowsInSection:indexPath.section] - 1;// section row 个数
    CGRect bounds = CGRectInset(cell.bounds, 12, 0); // 显示的cell 点击区域
    // 2.再盖一个 mask
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];// 用于蒙板
    // section 只有一个时。
    if (indexPath.row == 0 && indexPath.row == sectionCount) {
        [maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bounds cornerRadius:radius].CGPath];
        
        // 第一个 row
    } else if (indexPath.row == 0) {
        [maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bounds
                                                 byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
                                                       cornerRadii:CGSizeMake(radius, radius)].CGPath];
        
        // 最后一个 row
    } else if (indexPath.row == sectionCount) {
        [maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bounds
                                                 byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight)
                                                       cornerRadii:CGSizeMake(radius, radius)].CGPath];
        // 中间 row
    } else {
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:bounds];
        [maskLayer setPath:path.CGPath];
        
    }
    // 2.mask
    [cell setMaskView:[[UIView alloc] initWithFrame:cell.bounds]];
    [cell.maskView.layer insertSublayer:maskLayer atIndex:0];
    [cell.maskView.layer setMasksToBounds:YES];
    [cell setClipsToBounds:YES];


}


OK这样就行了  disPlaycell cell将要展示的时候会调用

0 0
原创粉丝点击