UI设计师有时候希望我们的产品比较酷。

阴影是他们喜欢的效果之一。

怎么设置阴影呢?

1、设置一个四边都相同的阴影

   UIImageView *testImgView = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];

    [testImgView setBackgroundColor:[UIColor yellowColor]];

    // 阴影颜色
testImgView.layer.shadowColor = [UIColor blackColor].CGColor;
// 阴影偏移,默认(0, -3)
testImgView.layer.shadowOffset = CGSizeMake(,);
// 阴影透明度,默认0
testImgView.layer.shadowOpacity = 0.5;
// 阴影半径,默认3
testImgView.layer.shadowRadius = ; [self.view addSubview:testImgView];

效果如图:

2、设置单边阴影

//单边阴影

    UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];

    [testLabel setBackgroundColor:[UIColor yellowColor]];

    // 阴影颜色
testLabel.layer.shadowColor = [UIColor blackColor].CGColor; // 阴影偏移,默认(0, -3)
testLabel.layer.shadowOffset = CGSizeMake(,); // 阴影透明度,默认0
testLabel.layer.shadowOpacity = 0.5;
// 阴影半径,默认3
testLabel.layer.shadowRadius = ; // 单边阴影 顶边
float shadowPathWidth = testLabel.layer.shadowRadius; CGRect shadowRect = CGRectMake(-shadowPathWidth/2.0, -shadowPathWidth/2.0, testLabel.bounds.size.width+shadowPathWidth, shadowPathWidth); UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
testLabel.layer.shadowPath = path.CGPath; [self.view addSubview:testLabel];

效果如下:

3、和阴影相关的属性

/** Shadow properties. **/

/* The color of the shadow. Defaults to opaque black. Colors created
* from patterns are currently NOT supported. Animatable. */ @property(nullable) CGColorRef shadowColor; /* The opacity of the shadow. Defaults to 0. Specifying a value outside the
* [0,1] range will give undefined results. Animatable. */ @property float shadowOpacity; /* The shadow offset. Defaults to (0, -3). Animatable. */ @property CGSize shadowOffset; /* The blur radius used to create the shadow. Defaults to 3. Animatable. */ @property CGFloat shadowRadius; /* When non-null this path defines the outline used to construct the
* layer's shadow instead of using the layer's composited alpha
* channel. The path is rendered using the non-zero winding rule.
* Specifying the path explicitly using this property will usually
* improve rendering performance, as will sharing the same path
* reference across multiple layers. Upon assignment the path is copied.
* Defaults to null. Animatable. */ @property(nullable) CGPathRef shadowPath;

UIView设置阴影的更多相关文章

  1. iPhone之为UIView设置阴影(CALayer的shadowColor,shadowOffset,shadowOpacity,shadowRadius,shadowPath属性)

    效果图: 以下代码实现: 第一个图片的代码 //加阴影--任海丽编辑 _imageView.layer.shadowColor = [UIColor blackColor].CGColor;//sha ...

  2. UIView 设置阴影(属性说明)

    以下代码实现: 第一个图片的代码 //加阴影--任海丽编辑 _imageView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowCo ...

  3. UIView设置阴影无效的原因之一

    本想在底部的按钮设置个阴影, 代码如下: self.layer.shadowColor = [UIColor blackColor].CGColor; self.layer.shadowOffset ...

  4. swift 设置阴影和圆角

    1.正常view设置阴影 func setShadow(view:UIView,sColor:UIColor,offset:CGSize, opacity:Float,radius:CGFloat) ...

  5. div四个边框分别设置阴影样式

    对于div边框的阴影一直没有很好地理解,也一直不明白怎么给四个边框分别设置阴影.昨天项目中碰到了这个问题,就认真想了一下,在此总结一二. 首先,还是从官方解释说起. 网上的解释通常都是什么水平阴影长度 ...

  6. iOS UIView设置圆角

    UIView设置圆角 1.比较简单的情况,UIView四个角都是圆角: UIView *aView = [[UIView alloc] init]; aView.frame = CGRectMake( ...

  7. UIView设置背景渐变色

    UIView设置背景渐变色 // Allocate bitmap context CGContextRef bitmapContext = CGBitmapContextCreate(NULL, , ...

  8. 使文字在div中水平和垂直居中的的css样式为,四个边分别设置阴影样式

    text-align:center; /*水平居中*/ line-height: 20px; /*行距设为与div高度一致*/ HTML元素 <div>水平垂直居中</div> ...

  9. IOS UIView圆角,阴影,边框,渐增光泽

    圆角 sampleView.layer.cornerRadius = 2.5; // 圓角的弧度sampleView.layer.masksToBounds = YES; 阴影 sampleView. ...

随机推荐

  1. Linux - CentOS 7 通过Yum源安装 MySql 5.7

    添加MySQL Yum存储库 从官网下载最新的mysql源 官网地址:https://dev.mysql.com/downloads/repo/yum/ 选择并下载适用于平台的发行包. 然后,在Lin ...

  2. Appium(六):元素定位

    1. 元素定位 对于自动化测试来说,核心技能就是对象的定位了.不管是web页面上的按钮或输入框,还是移动app上的一个按钮或输入框,我们要想对其进行点击或输入操作,前提是要先找到这个对象. webdr ...

  3. Spring Boot 静态资源能加载css 不能加载js

    Spring Boot 配置拦截器的时候默认 是放行 静态资源 , 也就是说不需要进行配置 registry.addResourceHandler("/**") .addResou ...

  4. 解决video.js不兼容ie8问题

    使用视频播放器的时候,常常会让兼容一些浏览器问题,比如兼容ie8浏览器.在工作中使用的是video.js. 如果需要兼容,引入两个js库,就可以做到兼容ie8浏览器 <script src=&q ...

  5. C# -- LinkedList的使用

    C# -- LinkedList的使用 private static void TestLinkList() { LinkedList<Person> linkListPerson = n ...

  6. django中使用原生的sql查询实例

    在app文件夹下创建database_operations.py文件,写如下内容: import pymysql from 项目名.settings import DATABASES class Da ...

  7. 【计算机网络】ISO/OSI 网络体系结构

    ISO/OSI 网络体系结构 计算机网络是相当复杂的系统,相互通信的两个计算机系统必须高度协调才能正常工作.为了设计这样复杂的计算机网络,人们提出了将网络分层的方法.分层可将庞大而复杂的问题转换为若干 ...

  8. linux下安装make工具

    安装make工具 make工具依赖gcc ,automake,autoconf,libtool,make 这些安装包 可以一起安装 center os系统 运行如下命令yum install gcc ...

  9. C#后台架构师成长之路-Orm篇体系

    成为了高工,只是完成体系的熟练,这个时候就要学会啃一些框架了... 常用Orm底层框架的熟悉: 1.轻量泛型的DBHelper,一般高工都自己写的出来的 2.EF-基于Linq的,好好用 3.Keel ...

  10. SQL Server通过条件搜索获取相关的存储过程等对象

    在SQL Server中,我们经常遇到一些需求,需要去搜索存储过程(Procedure).函数(Function)等对象是否包含某个对象或涉及某个对象,例如,我需要查找那些存储过程.函数是否调用了链接 ...