当前位置:首页 > 通信资讯 > 正文

ios 圆角阴影(ios图标阴影)

前言

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

先来看看效果图:

ios 圆角阴影(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)

总结

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

原文链接:http://www.jianshu.com/p/d7232dcb2259

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。