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. Zabbix Server 3.2

    软件环境 Centos7.3 LAMP Zabbix 3.2  1. Installing repository configuration package Install the repositor ...

  2. Appium(八):Appium API(二) 元素等待、元素操作

    1. 元素等待 我们在使用脚本的时候,可能会由于网络.服务器处理.电脑等原因,我们想要找的元素没有加载出来,这个时候如果直接定位就可能会报错. 这个时候我们就可以设置元素等待了. 什么叫元素等待呢? ...

  3. Java设计模式13:责任链模式

    前言 来菜鸟这个大家庭10个月了,总得来说比较融入了环境,同时在忙碌的工作中也深感技术积累不够,在优秀的人身边工作必须更加花时间去提升自己的技术能力.技术视野,所以开一个系列文章,标题就轻松一点叫做最 ...

  4. Python 之列表切片的四大常用操作

    最近在爬一个网站的文档的时候,老师要求把一段文字切割开来,根据中间的文本分成两段 故学习了一段时间的切片操作,现把学习成果po上来与大家分享 1.何为切片? 列表的切片就是处理列表中的部分元素,是把整 ...

  5. Appium+Java 自动化测试系列一:环境搭建

    Appium+Java 自动化测试框架搭建主要分为以下几个方面的下载安装及环境配置 1.Java开发环境 涉及到的内容又jdk.编译器工具(推荐jdk 1.8.Eclipse编译器或者IDEA编译工具 ...

  6. KnockoutJS-自定义属性绑定

    在knockoutjs中,已有的绑定功能已经十分强大,基本上可以不需要再去考虑扩展了,但是,也有例外的场景,面对这种场景,还是得去完成,knockoutJS提供了自定义绑定来扩展绑定功能. 一.新建绑 ...

  7. HeadFirst设计模式<1>

    HeadFirst设计模式<1> 1 策略模式 鸭子飞行和嘎嘎叫策略 2 工厂模式 简单工厂 工厂方法 抽象工厂 简单工厂简单的pizza工厂 通过一个工厂类的方法,创建和返回对象实例 原 ...

  8. React Hook父组件获取子组件的数据/函数

    我们知道在react中,常用props实现子组件数据到父组件的传递,但是父组件调用子组件的功能却不常用.文档上说ref其实不是最佳的选择,但是想着偷懒不学redux,在网上找了很多教程,要不就是hoo ...

  9. 1、nio说明 和 对比bio

    nio和bio的区别 bio: 面向流的. 单向的. 阻塞的,这也是b这个的由来. nio: 面向块的.(buffer) 双向的. 非阻塞的.同步的编程方式.是一种select模型 nio编程的常规步 ...

  10. 48-创建 overlay 网络

    上一节我们搭建好实验环境,配置并运行了consul,今天开始创建 overlay 网络. 在 host1 中创建 overlay 网络 ov_net1: -d overlay 指定 driver 为 ...