簡體   English   中英

頂部Tableview上的圓角不能使用陰影

[英]Shadow does not work with round corner on top Tableview

您好我的條件我使用兩個條件,一個只在頂部視圖后只有頂部視圖只在頂部的陰影,只有一個條件工作如果我添加陰影代碼它不起作用但我刪除角落代碼然后陰影工作請引導我

對於圓角我正在使用此擴展名

extension UIView {
    func roundCorners(corners:UIRectCorner, radius: CGFloat) {
    let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
    let mask = CAShapeLayer()
    mask.path = path.cgPath
    self.layer.mask = mask
}

並為陰影

func shadowToUIView( uiview : UIView){
    uiview.layer.shadowColor = UIColor.darkGray.cgColor
    uiview.layer.shadowOpacity = 0.50
    uiview.layer.shadowOffset = CGSize(width: 1, height: 1)
    uiview.layer.shadowRadius = 5
    uiview.layer.shouldRasterize = false
}

在行的單元格中我正在使用它

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomeTableViewCell
    cell.firstView.roundCorners(corners: [.topLeft,.topRight], radius: 25.0)
    shadowToUIView(uiview: cell.firstView)
    HomeMenu.openMenuCard(cell:cell, indexPath:indexPath, firstViewMain:firstViewMain, openedTab: openedTab)
    return cell
}

添加回復的代碼后

圖片

這是允許您使用陰影進行UIView舍入的方法:

extension UIView {
  func addShadow(cornerRadius: CGFloat, maskedCorners: CACornerMask, color: UIColor, offset: CGSize, opacity: Float, shadowRadius: CGFloat) {
    self.layer.cornerRadius = cornerRadius
    self.layer.maskedCorners = maskedCorners
    self.layer.shadowColor = color.cgColor
    self.layer.shadowOffset = offset
    self.layer.shadowOpacity = opacity
    self.layer.shadowRadius = shadowRadius
  }
}

用途:用於四舍五入:

<your_view_object>.addShadow(cornerRadius: 10.0, maskedCorners: [.layerMaxXMaxYCorner, .layerMinXMaxYCorner, .layerMaxXMinYCorner, .layerMinXMinYCorner], color: AppColors.themeBlack.withAlphaComponent(0.6), offset: CGSize.zero, opacity: 0.4, shadowRadius: 4.0)

僅用於圓角:

<your_view_object>.addShadow(cornerRadius: 10.0, maskedCorners: [.layerMaxXMinYCorner, .layerMinXMinYCorner], color: AppColors.themeBlack.withAlphaComponent(0.6), offset: CGSize.zero, opacity: 0.4, shadowRadius: 4.0)

僅用於倒圓角:

<your_view_object>.addShadow(cornerRadius: 10.0, maskedCorners: [.layerMaxXMaxYCorner, .layerMinXMaxYCorner], color: AppColors.themeBlack.withAlphaComponent(0.6), offset: CGSize.zero, opacity: 0.4, shadowRadius: 4.0)

根據您的要求制作陰影,更改其他參數。

用於layer.mask的Doc狀態:“圖層的alpha通道決定了圖層內容和背景的顯示程度。完全或部分不透明的像素允許底層內容顯示,但完全透明的像素會阻止該內容。默認值為屬性為nil。配置遮罩時,請記住設置遮罩層的大小和位置,以確保遮罩層與遮罩層正確對齊。“

所以,我補充道

    mask.frame = self.bounds.offsetBy(dx: -6.0, dy: -6.0)

正常工作(需要調整偏移值)。

好問題..!!! 我在最近的學校申請中也有同樣的要求。這是最好的解決方案,可以根據您的要求工作。 請在ShadowView類下面使用圓角陰影。

https://ibb.co/3S0rD7s

https://ibb.co/qCtrGqz

class ShadowView:UIView{[![enter image description here][1]][1]
    private var theShadowLayer: CAShapeLayer?
    override func layoutSubviews() {
        super.layoutSubviews()
            let rounding = CGFloat.init(10.0)
            var shadowLayer = CAShapeLayer.init()
            shadowLayer.name = "ShadowLayer1"
            shadowLayer.path = UIBezierPath.init(roundedRect: bounds, cornerRadius: rounding).cgPath
            shadowLayer.fillColor = UIColor.white.cgColor
            shadowLayer.shadowPath = shadowLayer.path
            shadowLayer.shadowColor = UIColor.init(red: 60.0/255.0, green: 64.0/255.0, blue: 67.0/255.0, alpha:0.3).cgColor
            shadowLayer.shadowRadius = CGFloat.init(2.0)
            shadowLayer.shadowOpacity = Float.init(0.5)
            shadowLayer.shadowOffset = CGSize.init(width: 0.0, height: 1.0)
            if  let arraySublayer1:[CALayer] = self.layer.sublayers?.filter({$0.name == "ShadowLayer1"}),let sublayer1 =  arraySublayer1.first{
                    sublayer1.removeFromSuperlayer()
            }
            self.layer.insertSublayer(shadowLayer, below: nil)
            shadowLayer = CAShapeLayer.init()
            shadowLayer.name = "ShadowLayer2"
            shadowLayer.path = UIBezierPath.init(roundedRect: bounds, cornerRadius: rounding).cgPath
            shadowLayer.fillColor = UIColor.white.cgColor
            shadowLayer.shadowPath = shadowLayer.path
            shadowLayer.shadowColor = UIColor.init(red: 60.0/255.0, green: 64.0/255.0, blue: 67.0/255.0, alpha:0.15).cgColor
            shadowLayer.shadowRadius = CGFloat.init(6.0)
            shadowLayer.shadowOpacity = Float.init(0.5)
            shadowLayer.shadowOffset = CGSize.init(width: 0.0, height: 2.0)
            if  let arraySublayer2:[CALayer] = self.layer.sublayers?.filter({$0.name == "ShadowLayer2"}),let sublayer2 =  arraySublayer2.first{
                sublayer2.removeFromSuperlayer()
            }
            self.layer.insertSublayer(shadowLayer, below: nil)

    }
}

易於實施
簡單的設計使一個Containerview繼承類ShadowView並添加Sub容器視圖(圓角視圖)..就是這樣.. !!! ..運行它.. !!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM