UIView贝塞尔曲线切割既有圆角又有阴影

1,687 阅读1分钟

.h

@interface ESShapeView : UIView

@end

.m

@implementation ESShapeView

+ (Class)layerClass {
    return [CAShapeLayer class];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        ((CAShapeLayer *)self.layer).fillColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1.0].CGColor;
    }
    return self;
}
//需要的形状
-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat wWidth = IPHONE_WIDTH-70;
    CGFloat viewHeight = CGRectGetHeight(self.frame);
    UIBezierPath * path = [UIBezierPath bezierPath];
    
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(0, 360)];
    [path addArcWithCenter:CGPointMake(0, 360+15) radius:15 startAngle:M_PI*3/2.f endAngle:M_PI_2 clockwise:YES];
    [path addLineToPoint:CGPointMake(0, viewHeight)];
    [path addLineToPoint:CGPointMake(wWidth, viewHeight)];
    [path addArcWithCenter:CGPointMake(wWidth, 360+15) radius:15 startAngle:M_PI_2 endAngle:M_PI*3/2.f clockwise:YES];
    [path addLineToPoint:CGPointMake(wWidth, 0)];
    [path addLineToPoint:CGPointMake(0, 0)];
    
    ((CAShapeLayer *)self.layer).path = path.CGPath;
    
}

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    ((CAShapeLayer *)self.layer).fillColor = backgroundColor.CGColor;
}
@end

使用

@property (retain, nonatomic) ESShapeView *codeBGView;
    self.codeBGView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    self.codeBGView.layer.shadowOffset = CGSizeMake(0, 0);
    self.codeBGView.layer.shadowRadius = 4;
    self.codeBGView.layer.shadowOpacity = 0.5;