如何同时为UIView添加顶角,顶部边框和顶部阴影

时间:2016-05-16 08:18:02

标签: ios objective-c core-graphics drawrect rounded-corners

我需要在UIView上添加几种效果,在其圆角曲线上将其左上角和右上角,白色边框和暗阴影四舍五入,如下图所示: enter image description here

我创建了一个从UIView继承的MSHTablePaneCell类,然后覆盖了它的drawRect方法:

static const CGFloat kCornerRadius = 15;
@implementation MSHTablePaneCell ()
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    UIBezierPath *path = [UIBezierPath    bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerTopLeft |UIRectCornerTopRight) cornerRadii:CGSizeMake(kCornerRadius, kCornerRadius)];

    // 1. mask round corner
    CAShapeLayer *roundLayer = [CAShapeLayer layer];
    roundLayer.path = path.CGPath;
    roundLayer.frame = self.bounds;
    //    roundLayer.cornerRadius = 10.0;
    self.layer.mask = roundLayer;

   // 2. add border to round corner curve
   UIBezierPath *topCurvepath = [UIBezierPath bezierPath];
   [topCurvepath moveToPoint:CGPointMake(0, kCornerRadius)];
   [topCurvepath addQuadCurveToPoint:CGPointMake(kCornerRadius, 0) controlPoint:CGPointMake(0, 0)];
   [topCurvepath addLineToPoint:CGPointMake(CGRectGetWidth(self.frame)-kCornerRadius, 0)];
   [topCurvepath addQuadCurveToPoint:CGPointMake(CGRectGetWidth(self.frame), kCornerRadius) controlPoint:CGPointMake(CGRectGetWidth(self.frame), 0)];

   CAShapeLayer *topBorderLayer = [CAShapeLayer layer];
   topBorderLayer.lineWidth = 4.0;
   topBorderLayer.borderColor = [UIColor redColor].CGColor;
   topBorderLayer.path = topCurvepath.CGPath;
   [self.contentView.layer addSublayer:topBorderLayer];
}
@end

但是,通过上面的代码,我得到了如下的最终图片: enter image description here

无论我设置哪个属性,topCurvePath只显示黑色背景颜色,我如何解决这个问题?提前谢谢〜

1 个答案:

答案 0 :(得分:0)

topBorderLayer.fillColor = [UIColor yourColor] .CGColor