Tableview Xib Shadow Swift

时间:2019-01-01 12:59:43

标签: ios css swift uitableview

这是我要尝试的操作:

Original Screen Shot

屏幕截图来自Iphone:

Taken Screenshot

这是我的代码:

cell.shadowLayerView.layer.masksToBounds = false
cell.shadowLayerView.layer.shadowOffset = CGSize(width: 0, height: 0)
cell.shadowLayerView.layer.shadowColor = UIColor.black.cgColor
cell.shadowLayerView.layer.shadowOpacity = 0.23
cell.shadowLayerView.layer.shadowRadius = 4
cell.shadowLayerView.layer.shadowPath = UIBezierPath(roundedRect: cell.shadowLayerView.bounds, cornerRadius: 2).cgPath
cell.shadowLayerView.layer.shouldRasterize = true
cell.shadowLayerView.layer.rasterizationScale = UIScreen.main.scale
cell.discriptionLbl.frame.size.width = UIScreen.main.bounds.size.width

这是我的表格视图xib。在原始图像中,所有侧面(顶部,底部,左侧,左侧,右侧)均显示浅灰色阴影,而在拍摄图像中,则显示所有侧面阴影,但为什么在右侧和底部均显示多余阴影。

请参见下图:

Latest Taken Image

问题:如何在视图的各个侧面显示与原始图像一样的阴影(浅灰色阴影)?

有人可以向我解释如何解决此问题,我已经尝试解决此问题,但没有结果。

任何帮助将不胜感激。

谢谢。

2 个答案:

答案 0 :(得分:1)

请参见以下代码:

mainViewCorner.layer.shadowColor = UIColor.red.cgColor;
mainViewCorner.layer.shadowOffset = CGSize.zero //direction of shadow
mainViewCorner.layer.shadowOpacity = 1.0; // opacity for shadow
mainViewCorner.layer.shadowRadius = 0.0; //amount of shadow to blur
mainViewCorner.layer.cornerRadius = 4.0;
带有您代码的

问题是shadowOffset。它定义了阴影的方向。如果要在所有侧面都阴影-宽度和高度都必须为零。

答案 1 :(得分:0)

尝试

    struct myStruct{
        std::shared_ptr<obj> mPtr;
        int x;
    }

    vector<myStruct> listA
    vector<std::shared_ptr<obj>> listB
    vector<myStruct>result
    std::sort(listA.begin(),listA.end());
    std::sort<listB.begin(),listB.end());

    std::set_difference(listA.begin().listA.end(),listB.begin(),listB.end(),std::back_inserter(result),[](vector<myStruct>::value_type &a,vector<std::shared_ptr<obj>>::value_type  &b){return a.mPtr != b;});

或者您可以使用Sketch Shadow

   myView.layer.cornerRadius = 10
   let shadowPath = UIBezierPath(roundedRect: myView.bounds, cornerRadius: 10)

    myView.layer.masksToBounds = false
    myView.layer.shadowColor = UIColor.gray.cgColor
    myView.layer.shadowOffset = CGSize(width: 0, height: 2)
    myView.layer.shadowOpacity = 0.5

应用草图阴影

extension CALayer {
    func applySketchShadow(
        color: UIColor = .black,
        alpha: Float = 0.5,
        x: CGFloat = 0,
        y: CGFloat = 2,
        blur: CGFloat = 4,
        spread: CGFloat = 0)
    {
        shadowColor = color.cgColor
        shadowOpacity = alpha
        shadowOffset = CGSize(width: x, height: y)
        shadowRadius = blur / 2.0
        if spread == 0 {
            shadowPath = nil
        } else {
            let dx = -spread
            let rect = bounds.insetBy(dx: dx, dy: dx)
            shadowPath = UIBezierPath(rect: rect).cgPath
        }
    }
}