【问题标题】:How to add shadow to UIView?如何给 UIView 添加阴影?
【发布时间】:2018-07-19 01:07:32
【问题描述】:

For UIView 我给出了两侧的圆角半径并添加了阴影。现在,我得到了两侧的圆角半径,而不是阴影。

这是我使用的代码:

@IBOutlet var myView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
   let shadowpath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 
   myView.frame.height, height: myView.frame.height), byRoundingCorners: 
   [.topRight, .bottomRight], cornerRadii: CGSize(width: 58.0, height: 0.0))

    myView.layer.shadowColor = UIColor.black.cgColor
    myView.layer.shadowOffset = CGSize(width: 0.5, height: 0.4)  //Here you 
    control x and y
    myView.layer.shadowOpacity = 0.5
    myView.layer.shadowRadius = 5.0 //Here your control your blur
    myView.layer.masksToBounds =  false
    myView.layer.shadowPath = shadowpath.cgPath

截图:

【问题讨论】:

  • 请显示该代码的其余部分。你也屏蔽图层吗?绘制圆角形状的代码在哪里?
  • 期望什么?我看不出有什么问题。
  • @user28434 应为屏幕截图结果
  • 好的,那么你能展示一下现在发生了什么吗?就像当前状态截图一样。
  • @user28434 是的,当然!

标签: ios swift swift3 uiview


【解决方案1】:

Swift 5+ 解决方案

extension UIView{
    func dropShadowWithCornerRaduis() {
        layer.masksToBounds = true
        layer.cornerRadius = 25
        layer.shadowColor = UIColor.gray.cgColor
        layer.shadowOpacity = 0.5
        layer.shadowOffset = CGSize(width: 0, height: 0)
        layer.shadowRadius = 1
        layer.shouldRasterize = true
        layer.rasterizationScale = UIScreen.main.scale
       
    }
}

用法

contentView.dropShadowWithCornerRaduis()

【讨论】:

    【解决方案2】:

    添加 Swift 5+ 解决方案:

    let tempView = UIView(frame: CGRect(x: 0, y: -50, width: self.view.frame.size.width/1.4, height: 300))
    
    //below is the code you need to add, adjust Opacity as needed
            tempView.layer.shadowColor = UIColor.black.cgColor
            tempView.layer.shadowOpacity = 0.8
            tempView.layer.shadowOffset = .zero
            tempView.layer.shadowRadius = 10
    

    【讨论】:

      【解决方案3】:

      直接从情节提要中使用的简单扩展。斯威夫特 4+

      @IBDesignable extension UIView {
          @IBInspectable var shadowColor: UIColor?{
              set {
                  guard let uiColor = newValue else { return }
                  layer.shadowColor = uiColor.cgColor
              }
              get{
                  guard let color = layer.shadowColor else { return nil }
                  return UIColor(cgColor: color)
              }
          }
      
          @IBInspectable var shadowOpacity: Float{
              set {
                  layer.shadowOpacity = newValue
              }
              get{
                  return layer.shadowOpacity
              }
          }
      
          @IBInspectable var shadowOffset: CGSize{
              set {
                  layer.shadowOffset = newValue
              }
              get{
                  return layer.shadowOffset
              }
          }
      
          @IBInspectable var shadowRadius: CGFloat{
              set {
                  layer.shadowRadius = newValue
              }
              get{
                  return layer.shadowRadius
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        我自己通过给 layerMaxXMinYCorner 和 layerMaxXMaxYCorner 找到了答案

           myView.clipsToBounds = true
            myView.layer.cornerRadius = 58
            if #available(iOS 11.0, *) {
                myView.layer.maskedCorners = [.layerMaxXMinYCorner, 
          .layerMaxXMaxYCorner ]
            } else {
                // Fallback on earlier versions
            }
        
            let shadowpath = UIBezierPath(roundedRect: self.myView.bounds, 
            byRoundingCorners: [.topRight, .bottomRight], cornerRadii: CGSize(width: 
           58.0, height: 0.0))
        
        
        
            myView.layer.shadowColor = UIColor.black.cgColor
            myView.layer.shadowOffset = CGSize(width: 1, height: 1)  //Here you 
            control x and y
            myView.layer.shadowOpacity = 0.5
            myView.layer.shadowRadius = 15 //Here your control your blur
            myView.layer.masksToBounds =  false
            myView.layer.shadowPath = shadowpath.cgPath
        

        【讨论】:

          【解决方案5】:

          关键是使用“容器”视图...你添加一个“阴影层”作为容器视图的子层,并将蒙版视图添加为容器的子视图。

          这是一个您可以在 Playground 中运行的示例,它将为您提供所展示的目标(您可能需要调整颜色值、阴影半径和偏移量):

          import UIKit
          import PlaygroundSupport
          
          class TestViewController : UIViewController {
          
              override func viewDidLoad() {
                  super.viewDidLoad()
          
                  view.backgroundColor = UIColor.init(white: 0.8, alpha: 1.0)
          
                  let myView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
                  myView.backgroundColor = .white
          
                  let mask = CAShapeLayer()
          
                  let shadowpath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width:
                      myView.frame.height, height: myView.frame.height), byRoundingCorners:
                      [.topRight, .bottomRight], cornerRadii: CGSize(width: 58.0, height: 0.0))
          
                  mask.path = shadowpath.cgPath
                  myView.layer.mask = mask
          
                  let shadowLayer = CAShapeLayer()
                  shadowLayer.frame = myView.bounds
                  shadowLayer.path = shadowpath.cgPath
                  shadowLayer.shadowOpacity = 0.5
                  shadowLayer.shadowRadius = 5
                  shadowLayer.shadowColor = UIColor(red: 0.2, green: 0.5, blue: 1.0, alpha: 1.0).cgColor
                  shadowLayer.masksToBounds = false
                  shadowLayer.shadowOffset = CGSize(width: 5.0, height: 1.0)
          
                  let container = UIView(frame: CGRect(x: 40, y: 100, width: myView.bounds.width, height: myView.bounds.height))
                  container.backgroundColor = .clear
                  container.layer.addSublayer(shadowLayer)
                  container.addSubview(myView)
          
                  view.addSubview(container)
          
              }
          
          }
          
          let vc = TestViewController()
          PlaygroundPage.current.liveView = vc
          

          结果:

          【讨论】:

            猜你喜欢
            • 2015-10-23
            • 1970-01-01
            • 1970-01-01
            • 2016-09-30
            • 2020-05-21
            • 2012-06-07
            • 2014-07-30
            • 2013-08-26
            相关资源
            最近更新 更多