Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 带阴影的分组表视图_Ios_Swift_Uitableview_Shadow - Fatal编程技术网

Ios 带阴影的分组表视图

Ios 带阴影的分组表视图,ios,swift,uitableview,shadow,Ios,Swift,Uitableview,Shadow,我想实现一个分组的tableView和阴影,如下图所示 为了将圆角和阴影一起归档,我只需在主视图后面添加一个单独的UIView,并从四面将其与主视图固定在一起,如下所示 在阴影视图上应用阴影,并通过主视图上的遮罩根据单元的索引圆角。这种方法成功地实现了带阴影的遮罩圆角 但细胞的阴影是重叠的: 为了解决这个问题,顶部单元格的顶部、右侧和左侧都应该有阴影,中间单元格的右侧和左侧都应该有阴影,底部单元格的右侧、左侧和底部都应该有阴影。我花了很长时间才弄清楚这一点,最后修改了一些Objc代码以获得您想要

我想实现一个分组的tableView和阴影,如下图所示

为了将圆角和阴影一起归档,我只需在主视图后面添加一个单独的UIView,并从四面将其与主视图固定在一起,如下所示

在阴影视图上应用阴影,并通过主视图上的遮罩根据单元的索引圆角。这种方法成功地实现了带阴影的遮罩圆角

但细胞的阴影是重叠的:


为了解决这个问题,顶部单元格的顶部、右侧和左侧都应该有阴影,中间单元格的右侧和左侧都应该有阴影,底部单元格的右侧、左侧和底部都应该有阴影。

我花了很长时间才弄清楚这一点,最后修改了一些Objc代码以获得您想要的结果

我从来没有意愿让它也适用于单个细胞。总是有一些虫子。但是,对于两个或多个单元格,请在单元格本身上使用此扩展名,而不要在
contentView
上使用此扩展名

单元格内部
layoutsubview

backgroundView = UIView(frame: contentView.frame)
backgroundView?.clipsToBounds = false
backgroundView?.backgroundColor = .clear

addShadowToCellInTableView(lastIndex: lastIndex, atIndexPath: indexPath)

实现这一点的一种方法是自定义阴影路径。看一看。您将需要一个函数向边添加阴影,以及两个在顶部和底部边缘添加阴影。然后根据要添加阴影的单元格使用正确的函数…另外,请添加用于自定义单元格的代码和
表格视图(u:cellForRowAt:)
代码。
public extension UITableViewCell {
    /** adds a drop shadow to the background view of the (grouped) cell */
    func addShadowToCellInTableView(lastIndex: Int, atIndexPath indexPath: IndexPath!) {
        let isFirstRow: Bool = indexPath.row == 0
        let isLastRow: Bool = (indexPath.row == lastIndex - 1)

        guard let backgroundView = self.backgroundView else { return }

        let backBounds = backgroundView.bounds
        // the shadow rect determines the area in which the shadow gets drawn
        var shadowRect: CGRect = backBounds.insetBy(dx: 0, dy: -10)
        if isFirstRow {
            shadowRect.origin.y += 10
        } else if isLastRow {
            shadowRect.size.height -= 10
        }

        // the mask rect ensures that the shadow doesn't bleed into other table cells
        var maskRect: CGRect = backBounds.insetBy(dx: -20, dy: 0)
        if isFirstRow {
            maskRect.origin.y -= 10
            maskRect.size.height += 10
        } else if isLastRow {
            maskRect.size.height += 10
        }

        // now configure the background view layer with the shadow
        let layer: CALayer = backgroundView.layer
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOffset = CGSize(width: 0, height: 0)
        layer.shadowRadius = 4
        layer.shadowOpacity = 0.23
        layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath
        layer.masksToBounds = false

        // and finally add the shadow mask
        let maskLayer = CAShapeLayer()
        maskLayer.path = UIBezierPath(rect: maskRect).cgPath
        layer.mask = maskLayer
    }

    func addShadowToSingleCell() {
        layer.shadowOpacity = 0.23
        layer.shadowRadius = 4
        layer.shadowOffset = CGSize(width: 0, height: 0)
        layer.shadowColor = UIColor.black.cgColor

        layer.shadowPath = UIBezierPath(roundedRect: contentView.frame,
                                        cornerRadius: contentView.layer.cornerRadius).cgPath
    }
}