当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS如何为圆角添加阴影效果示例代码

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

ca1824,saiwo,将军在上在线观看

前言

大家都知道在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)

总结

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网