iOS如何为圆角添加阴影效果示例代码

2025-05-29 0 79

前言

大家都知道在ios中为uiview添加阴影还是比较简单的,只需要设置layer的shadow属性就可以了,但是问题在于设置阴影之后,必须设置maskstobounds为no,而圆角图片则要求maskstobounds必须为yes,两者相互冲突,会导致无法正确的添加阴影。下面就来给大家介绍正确为圆角添加阴影的效果,话不多说了,来一起看看详细的介绍吧。

先来看看效果图:

iOS如何为圆角添加阴影效果示例代码

正确的做法:

先创建一个透明的uiview,并添加阴影,设置maskstobounds为no;

然后在透明的uiview上添加圆角图片,在subview上设置maskstobounds为yes;

这样,就可以完美实现对应的阴影了。

示例代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
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)

总结

以上就是这篇文章的全部内容了,本文还有许多不足,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS如何为圆角添加阴影效果示例代码 https://www.kuaiidc.com/89461.html

相关文章

发表评论
暂无评论