如何为圆角添加阴影
2018-03-29 15:56
323 查看
在iOS中为UIView添加阴影还是比较简单的,只需要设置
正确的做法是先创建一个透明的
然后在透明的
这样,就可以完美实现对应的阴影了。
layer的
shadow属性就可以了,但是问题在于设置阴影之后,必须设置
masksToBounds为
NO,而圆角图片则要求
masksToBounds必须为
YES,两者相互冲突,会导致无法正确的添加阴影。
正确的做法是先创建一个透明的
UIView,并添加阴影,设置
masksToBounds为
NO;
然后在透明的
UIView上添加圆角图片,在
subView上设置
masksToBounds为
YES;
这样,就可以完美实现对应的阴影了。
let baseView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100)) // add the shadow to the base view baseView.backgroundColor = UIColor.clear baseView.layer.shadowColor = UIColor.black.cgColor baseView.layer.shadowOffset = CGSize(width: 3, height: 3) baseView.layer.shadowOpacity = 0.7 baseView.layer.shadowRadius = 4.0 self.view.addSubview(baseView) // add any other subcontent that you want clipped let otherSubContent = UIImageView() otherSubContent.image = UIImage(named: "lion") otherSubContent.frame = baseView.bounds otherSubContent.layer.masksToBounds = true otherSubContent.layer.cornerRadius = 50 baseView.addSubview(otherSubContent)效果如下图:
相关文章推荐
- iOS如何为圆角添加阴影效果示例代码
- android对位图进行处理,增加圆角的同时,在右侧和下侧添加阴影。
- 如何得到一个 圆角 边框 。 阴影
- Flex中如何利用seriesFilters属性,添加/删除LineChart图表曲线阴影
- IOS中View添加阴影/圆角的方法
- UIView添加圆角阴影
- 如何在圆形 imageView android 上添加一个阴影和边界?
- iOS中如何优雅的添加圆角和边框?
- 如何为控件添加阴影效果
- 添加 阴影 圆角
- React Native之如何在Android上添加阴影
- iOS中如何优雅的添加圆角和边框?
- 1.给图像添加阴影、边框和圆角 2。CALayer的隐式动画
- 给uiview添加阴影或添加圆角
- 如何给物体添加阴影效果
- Swift开发教程--如何给UITextView添加圆角边框
- 将视图设置成圆角和阴影添加以及UIButton定义和设置圆角
- [Unity3D]如何给物体添加阴影效果
- 如何创建圆角 UITextField 与内阴影