圆角阴影

2018-06-13  本文已影响0人  foolish_hungry

全圆角阴影

@implementation TPShadowView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kUIColorFromRGB(0x4b4773);
        //v.layer.masksToBounds=YES;这行去掉
        self.layer.cornerRadius = 10;
        self.layer.shadowColor = kUIColorFromRGB1(0x000000, 1).CGColor;
        self.layer.shadowOffset = CGSizeMake(2, 5);
        self.layer.shadowOpacity = 0.5;
        self.layer.shadowRadius = 5;

    }
    return self;
}

部分圆角阴影

#pragma mark - setters and getters
- (UIView *)iShadowCornerView {
    if (!_iShadowCornerView) {
        _iShadowCornerView = [[UIView alloc] init];
        _iShadowCornerView.backgroundColor = [UIColor whiteColor];
        
        // 设置部分圆角
        CGRect rect = CGRectMake(0, 0, UIScreenWidth - 30, 125);
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect
                                                   byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
                                                         cornerRadii:CGSizeMake(5, 5)];
        CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
        shapeLayer.frame = rect;
        shapeLayer.cornerRadius = 5;
        shapeLayer.path = maskPath.CGPath;
        shapeLayer.fillColor = [UIColor whiteColor].CGColor;
        [_iShadowCornerView.layer addSublayer:shapeLayer];
        
        // 添加阴影
        _iShadowCornerView.layer.cornerRadius = 5;
        _iShadowCornerView.layer.shadowRadius = 5;
        _iShadowCornerView.layer.shadowOpacity = 0.5;
        _iShadowCornerView.layer.shadowOffset = CGSizeMake(2, -2);
        _iShadowCornerView.layer.shadowColor = kColorHexA(0x000000, 0.2).CGColor;
    }
    return _iShadowCornerView;
}

swift 版

/// 添加部分圆角 和 阴影
    public func addPartRoundCorner(rectCorner: UIRectCorner, cornerRadius: CGFloat, shadowColor: UIColor, shadowOffSet: CGSize, shadowOpacity: Float = 1) {
        // 设置部分圆角
        let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: rectCorner, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
        let shapeLayer = CAShapeLayer()
        shapeLayer.frame = self.bounds
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.path = maskPath.cgPath
        self.layer.addSublayer(shapeLayer)
        // 添加阴影
        self.layer.cornerRadius = cornerRadius
        self.layer.shadowRadius = cornerRadius
        self.layer.shadowOpacity = shadowOpacity
        self.layer.shadowOffset = shadowOffSet
        self.layer.shadowColor = shadowColor.cgColor
    }
上一篇 下一篇

猜你喜欢

热点阅读