您的位置:首页 > 移动开发 > IOS开发

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

2017-04-18 15:02 477 查看
需求图是这样的


整个是个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将要展示的时候会调用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: