源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-03-16 12:56 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:iOS如何为圆角添加阴影效果示例代码
[b]前言[/b] 大家都知道在iOS中为UIView添加阴影还是比较简单的,只需要设置layer的shadow属性就可以了,但是问题在于设置阴影之后,必须设置masksToBounds为NO,而圆角图片则要求masksToBounds必须为YES,两者相互冲突,会导致无法正确的添加阴影。下面就来给大家介绍正确为圆角添加阴影的效果,话不多说了,来一起看看详细的介绍吧。 [b]先来看看效果图:[/b] [img]http://files.jb51.net/file_images/article/201710/2017101992020531.png?201791992028[/img] [b]正确的做法:[/b] 先创建一个透明的UIView,并添加阴影,设置masksToBounds为NO; 然后在透明的UIView上添加圆角图片,在subView上设置masksToBounds为YES; 这样,就可以完美实现对应的阴影了。 [b]示例代码[/b]
  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)
[b]总结[/b] 以上就是这篇文章的全部内容了,本文还有许多不足,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对编程素材网的支持。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部