美文网首页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 视图添加阴影

    1、四边加阴影 2、顶部加阴影 来源:https://www.jianshu.com/p/d299ddf5aaaf...

  • iOS中给view设置阴影效果

    iOS开发中我们经常会遇到给指定视图设置其阴影效果 今天就来简单整理一下这里只是简单的给视图添加上阴影的效果 简单...

  • UIView圆角+阴影

    backView是要添加阴影的视图,思路是backView设置corner圆角,再添加一个带阴影的父视图,代码如下...

  • 让CALayer的shadowPath跟随bounds一起做动画

    前言 在iOS开发中,我们经常需要给视图添加阴影效果,最简单的方法就是通过设置CALayer的shadowColo...

  • iOS 在圆角视图上添加阴影

    阴影其实就是在视图之外添加了一层类似遮罩的图层 所以只要是设置了layer.masksToBounds的视图都是无...

  • 记录问题

    iOS11 之后,添加导航栏之后,视图添加在self.view 上面之后,视图的位置是0 0 开始的,结果视图添加...

  • iOS核心动画-阴影

    在iOS中常见的特性中有有阴影,下面我将详细阐述一下关于视图阴影的故事iOS中阴影的属性是shadowOpacit...

  • 000-CALayer阴影

    1、通过设置CALayer来给视图控件添加阴影 2、效果如下:

  • iOS中给文字添加阴影

    iOS 中给文字添加阴影: NSMutableAttributedString *attributedString...

  • 视图添加阴影的常见问题解析

    如何给视图(UIView)添加阴影,相信大家都已经轻车熟路;参考代码如下: 常见问题 阴影不可见检查layer的m...

网友评论

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

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