【问题标题】:How to give bottom shadow to UIView in Swift 3?如何在 Swift 3 中为 UIView 提供底部阴影?
【发布时间】:2018-03-01 02:46:06
【问题描述】:

这是我想要的

我的代码

let shadowPath = UIBezierPath(rect: shadowView.bounds).cgPath
shadowView.layer.shadowColor = UIColor.grey.cgColor
shadowView.layer.shadowOffset = CGSize(width: 0, height: 1)
shadowView.layer.shadowOpacity = 0.5
shadowView.layer.masksToBounds = false
shadowView.layer.shadowPath = shadowPath
shadowView.layer.shadowRadius = 5

我想在我的 UIVIew 中实现完全相同的阴影。当我使用上面的代码运行我的应用程序时,它分布在 4 个方面。注意:阴影在两个边缘很薄,在中心是粗体。

【问题讨论】:

  • 你的影子应该是红色的?我找不到红色
  • 你看到的是哪种颜色?我将不透明度设为 0.5。不在乎颜​​色。
  • 灰色?图片周围
  • 我只想构建阴影形状而不是颜色
  • 我给红色测试...

标签: ios swift swift3 uiview


【解决方案1】:

尝试创建自定义路径:

let shadowPath = UIBezierPath()
shadowPath.move(to: CGPoint(x: someView.bounds.origin.x, y: someView.frame.size.height))
shadowPath.addLine(to: CGPoint(x: someView.bounds.width / 2, y: someView.bounds.height + 7.0))
shadowPath.addLine(to: CGPoint(x: someView.bounds.width, y: someView.bounds.height))
shadowPath.close()

shadowView.layer.shadowColor = UIColor.darkGray.cgColor
shadowView.layer.shadowOpacity = 1
shadowView.layer.masksToBounds = false
shadowView.layer.shadowPath = shadowPath.cgPath
shadowView.layer.shadowRadius = 5

这给出了类似的结果,请尝试自定义点位置和其他值以满足您的需求。

【讨论】:

    【解决方案2】:

    对@jonaszmclaren 稍作改动以提供矩形阴影:

    func makeBottomShadow(forView view: UIView, shadowHeight: CGFloat = 5) {
        let shadowPath = UIBezierPath()
        shadowPath.move(to: CGPoint(x: 0, y: view.bounds.height))
        shadowPath.addLine(to: CGPoint(x: view.bounds.width, y: view.bounds.height))
        shadowPath.addLine(to: CGPoint(x: view.bounds.width, y: view.bounds.height + shadowHeight))
        shadowPath.addLine(to: CGPoint(x: 0, y: view.bounds.height + shadowHeight))
        shadowPath.close()
    
        view.layer.shadowColor = UIColor.darkGray.cgColor
        view.layer.shadowOpacity = 0.5
        view.layer.masksToBounds = false
        view.layer.shadowPath = shadowPath.cgPath
        view.layer.shadowRadius = 2
    }
    

    【讨论】:

      【解决方案3】:

      试试这个

      IBOutlet

      @IBOutlet weak var vwShadow: UIView!
      

      在 ViewdidLoad 中

      vwShadow.layer.shadowColor = UIColor.gray.cgColor
      vwShadow.layer.masksToBounds = false
      vwShadow.layer.shadowOffset = CGSize(width: 0.0 , height: 5.0)
      vwShadow.layer.shadowOpacity = 1.0
      vwShadow.layer.shadowRadius = 1.0
      

      【讨论】:

      • 这不是我想要的。
      • 正如您在上面所说的,您希望在底部隐藏视图。这是它的代码 sn-p。
      • 我只想要底部的阴影。您的代码将阴影略微扩散到右侧、左侧和顶部。那不是我需要的。
      • 它可以以编程方式完成。 @jonaszmclaren 帮助我实现了这一目标。
      • 这会为所有 4 个面添加阴影。
      猜你喜欢
      • 2017-09-02
      • 2023-01-29
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多