美文网首页iOS常用
iOS 视图添加阴影

iOS 视图添加阴影

作者: 三岁就很乖 | 来源:发表于2020-10-14 16:18 被阅读0次

    1、四边加阴影


    四边加阴影

    2、顶部加阴影


    顶部加阴影
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        UIView *floatView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
        floatView.backgroundColor = [UIColor systemPinkColor];
        [self.view addSubview:floatView];
    //    [self addFourSidesShadowToView:floatView withColor:[UIColor blackColor]];
        [self addSingleSidesShadowToView:floatView withColor:[UIColor blackColor]];
    }
    //添加四边阴影效果
    -(void)addFourSidesShadowToView:(UIView *)theView withColor:(UIColor*)theColor{
        //阴影颜色
        theView.layer.shadowColor = theColor.CGColor;
        //阴影偏移
        theView.layer.shadowOffset = CGSizeMake(0, 0 );
        //阴影透明度,默认0
        theView.layer.shadowOpacity = 0.3;
        //阴影半径,默认3
        theView.layer.shadowRadius = 8;
    }
    //在顶部添加阴影
    -(void)addSingleSidesShadowToView:(UIView *)theView withColor:(UIColor*)theColor{
        //阴影颜色
        theView.layer.shadowColor = theColor.CGColor;
        //阴影偏移
        theView.layer.shadowOffset = CGSizeMake(0, 0 );
        //阴影透明度,默认0
        theView.layer.shadowOpacity = 0.3;
        //阴影半径,默认3
        theView.layer.shadowRadius = 8;
        //单边阴影
        CGFloat shadowPathWidth = theView.layer.shadowRadius;
        CGRect shadowRect = CGRectMake(0, 0-shadowPathWidth/2, theView.bounds.size.width, shadowPathWidth);
        
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
        theView.layer.shadowPath = path.CGPath;
    }
    
    

    来源:https://www.jianshu.com/p/d299ddf5aaaf

    相关文章

      网友评论

        本文标题:iOS 视图添加阴影

        本文链接:https://www.haomeiwen.com/subject/deswpktx.html