UICollectionViewCell加虚线圆角边框

模拟器截图

加边框和圆角

UIBezierPath *maskPath;
CGRect boardRect;

float h = cell.bounds.size.height;
float w = cell.bounds.size.width;

if (indexPath.row == 0) {
    boardRect = cell.bounds;
    maskPath = [[UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(10, 10)] bezierPathByReversingPath];
} else if (indexPath.row == 3) {
    boardRect = CGRectMake(-1, 0, w+1, h);
    maskPath = [[UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)] bezierPathByReversingPath];
} else if (indexPath.row == 4) {
    boardRect = CGRectMake(0, -1, w, h+1);
    maskPath = [[UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)] bezierPathByReversingPath];
} else {
    boardRect = CGRectMake(-1, 0, w+2, h);
    maskPath = [UIBezierPath bezierPathWithRect:boardRect];
}

for (CALayer *layer in cell.contentView.layer.sublayers) {
    if ([layer.name isEqualToString:@"maskLayer"]) {
        [layer removeFromSuperlayer];
    }
}

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];

maskLayer.name = @"maskLayer";
maskLayer.frame = boardRect;
maskLayer.path = maskPath.CGPath;
maskLayer.strokeColor = JXColorSeparatorDark.CGColor;
maskLayer.lineDashPattern = @[@4, @2];
maskLayer.lineWidth = 1.0f;
maskLayer.fillColor = [UIColor clearColor].CGColor;
maskLayer.masksToBounds = YES;

[cell.contentView.layer insertSublayer:maskLayer atIndex:0];

设置选中效果

在collectionViewCell.m中

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    
    UIColor *fillColor = selected == YES ? JXColorBack : [UIColor clearColor];

    
    for (CALayer *layer in self.contentView.layer.sublayers) {
        if ([layer.name isEqualToString:@"maskLayer"]) {
            
            CAShapeLayer *shape = (CAShapeLayer *)layer;
            
            shape.fillColor = fillColor.CGColor;
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、属性选择器:id选择器 # 通过id 来选择类名选择器 . 通过类名来选择属性选择器 ...
    Yuann阅读 1,658评论 0 7
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,638评论 25 708
  • 当邵夷贝与GALA还没唱出“虽然虽然很想你,想联络却迟疑;命中注定的相遇,都失之交臂。虽然虽然失去你,梦里仍有你;...
    有声默片丶阅读 921评论 3 4
  • 1、好种子 a.在腾讯公益上为先心儿童捐款6元,祈祷所有孩子都能接受治疗,幸福快乐成长。 b.昨晚整晚都是给露宿者...
    我是一面镜子阅读 148评论 0 0
  • 我不知道我在坚持什么,他对我不怎么上心。无数次都想提分手,可是话到嘴边,又咽回了肚子。就算是晚上委屈哭到眼睛肿,第...
    暖小晨阅读 485评论 0 0