加边框和圆角
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;
}
}
}