view设置单边阴影

[Tool view:bottomV shadowPathWithColor:rgba(0, 0, 0, 0.09) shadowOpacity:1 shadowRadius:2.5 shadowPathType:1 shadowPathWidth:2];

+ (void)view:(UIView *)view shadowPathWithColor:(UIColor *)shadowColor shadowOpacity:(CGFloat)shadowOpacity shadowRadius:(CGFloat)shadowRadius shadowPathType:(NSInteger)shadowPathType shadowPathWidth:(CGFloat)shadowPathWidth{
    
    view.layer.masksToBounds = NO;//必须要等于NO否则会把阴影切割隐藏掉
    view.layer.shadowColor = shadowColor.CGColor;// 阴影颜色
    view.layer.shadowOpacity = shadowOpacity;// 阴影透明度,默认0
    view.layer.shadowOffset = CGSizeZero;//shadowOffset阴影偏移,默认(0, -3),这个跟shadowRadius配合使用
    view.layer.shadowRadius = shadowRadius;//阴影半径,默认3
    CGRect shadowRect = CGRectZero;
    CGFloat originX,originY,sizeWith,sizeHeight;
    originX = 0;
    originY = 0;
    sizeWith = view.bounds.size.width;
    sizeHeight = view.bounds.size.height;
    
    if (shadowPathType == 1) {
        //top
        shadowRect = CGRectMake(originX, originY-shadowPathWidth/2, sizeWith, shadowPathWidth);
    }else if (shadowPathType == 2){
        //bottom
        shadowRect = CGRectMake(originY, sizeHeight-shadowPathWidth/2, sizeWith, shadowPathWidth);
    }else if (shadowPathType == 3){
        //left
        shadowRect = CGRectMake(originX-shadowPathWidth/2, originY, shadowPathWidth, sizeHeight);
    }else if (shadowPathType == 4){
        //right
        shadowRect = CGRectMake(sizeWith-shadowPathWidth/2, originY, shadowPathWidth, sizeHeight);
    }else if (shadowPathType == 5){
        //普通
        shadowRect = CGRectMake(originX-shadowPathWidth/2, 2, sizeWith+shadowPathWidth, sizeHeight+shadowPathWidth/2);
    }else if (shadowPathType == 6){
        //圆
        shadowRect = CGRectMake(originX-shadowPathWidth/2, originY-shadowPathWidth/2, sizeWith+shadowPathWidth, sizeHeight+shadowPathWidth);
    }
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:shadowRect];
    view.layer.shadowPath = bezierPath.CGPath;//阴影路径
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容