【问题标题】:Shadow on UIVIewUIVIew 上的阴影
【发布时间】:2018-09-13 00:10:44
【问题描述】:

我正在尝试完成一个类似于的阴影

我尝试完成上述操作的代码如下,但它并不完全准确。

mainView.layer.shadowOpacity = 0.2
mainView.layer.shadowOffset = .zero
mainView.layer.shadowRadius = 3
mainView.layer.shadowColor = UIColor.darkGray.cgColor
mainView.layer.masksToBounds = false

知道如何复制图像的精确阴影吗?

【问题讨论】:

  • 应该如何衡量 nailed-it-ness?
  • 通过复制图像上的阴影。
  • 您可以提供您的代码生成的图像,在没有任何参考的情况下很难猜测您必须调整哪些值。
  • 您可能会在此链接中找到解决方法stackoverflow.com/a/34984063

标签: ios swift uiview shadow


【解决方案1】:

增加阴影不透明度:

    mainView.layer.cornerRadius = 3
    mainView.layer.shadowOpacity = 0.8
    mainView.layer.shadowOffset = CGSize(width: 2, height: 2)
    mainView.layer.shadowRadius = 3
    mainView.layer.shadowColor = UIColor.red.cgColor
    mainView.layer.masksToBounds = false

【讨论】:

    【解决方案2】:

    请使用这个简单的扩展

    extension UIView {
            func addShadow() {
                self.layer.masksToBounds = false
                self.layer.shadowColor = UIColor.black.cgColor
                self.layer.shadowOpacity = 0.4
                self.layer.shadowOffset = CGSize(width: 1, height: 2)
                self.layer.shadowRadius = 3
                self.layer.cornerRadius = 15
            }
         }
    

    【讨论】:

      【解决方案3】:

      不要忘记为视图添加背景颜色。

      mainView.backgroundColor = .white
      mainView.layer.cornerRadius = 5
      mainView.layer.shadowOpacity = 0.3
      mainView.layer.shadowOffset = CGSize(width: 0, height: 0)
      mainView.layer.shadowRadius = 3
      mainView.layer.shadowColor = UIColor.darkGray.cgColor
      mainView.layer.masksToBounds = false
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2017-07-31
        • 2016-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-08
        • 2011-11-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多