将阴影和圆角半径应用于视图,而不是整个单元格

apply shadow and corner radius to a view, not the entire cell

我有一个 table 视图单元格,在其中我有一个视图。在视图中,我有 2 个标签和一个图像。我希望视图具有阴影和圆角半径,而不是整个 table 视图单元格。请提出一些建议...

对于阴影,我会在实际视图后面创建另一个视图,并根据您希望阴影与视图成角度的位置为其指定不同的 x 或 y 位置。然后将您的主视图添加为该阴影视图的子类。通过更改 alpha 和背景颜色值将阴影视图配置为看起来像阴影。对于圆角半径,它由view.layer.cornerRadius = x

改变

确保将阴影视图的角半径也更改为等于主视图的角半径,这样看起来更逼真。

我相信我理解正确,如果我没有回答正确,请评论。

在您的代码中使用此 UIView 扩展。 Select 您的视图和情节提要中您可以设置属性值

    @IBDesignable extension UIView {

    /* The color of the shadow. Defaults to opaque black. Colors created
     * from patterns are currently NOT supported. Animatable. */
    @IBInspectable var shadowColor: UIColor? {
        set {
            layer.shadowColor = newValue!.cgColor
        }
        get {
            if let color = layer.shadowColor {
                return UIColor(cgColor:color)
            }
            else {
                return nil
            }
        }
    }
    @IBInspectable var clipBounds: Bool {
        set {
            clipsToBounds = newValue
        }
        get {
            return clipsToBounds
        }
    }
    @IBInspectable var cornerRadius: CGFloat {
        set {
            layer.cornerRadius = newValue
        } get {
            return layer.cornerRadius
        }
    }
    @IBInspectable var borderWidth: CGFloat {
        set {
            layer.borderWidth = newValue
        } get {
            return layer.borderWidth
        }
    }
    @IBInspectable var borderColor: UIColor? {
        set {
            layer.borderColor = newValue?.cgColor
        }
        get {
            if let color = layer.borderColor {
                return UIColor(cgColor:color)
            }
            else {
                return nil
            }
        }

    }
    @IBInspectable var borderColorCode: CGColor? {
        set {
            layer.borderColor = newValue
        } get {
            return layer.borderColor
        }
    }
    /* The opacity of the shadow. Defaults to 0. Specifying a value outside the
     * [0,1] range will give undefined results. Animatable. */
    @IBInspectable var shadowOpacity: Float {
        set {
            layer.shadowOpacity = newValue
        }
        get {
            return layer.shadowOpacity
        }
    }

    /* The shadow offset. Defaults to (0, -3). Animatable. */
    @IBInspectable var shadowOffset: CGPoint {
        set {
            layer.shadowOffset = CGSize(width: newValue.x, height: newValue.y)
        }
        get {
            return CGPoint(x: layer.shadowOffset.width, y:layer.shadowOffset.height)
        }
    }

    /* The blur radius used to create the shadow. Defaults to 3. Animatable. */
    @IBInspectable var shadowRadius: CGFloat {
        set {
            layer.shadowRadius = newValue
        }
        get {
            return layer.shadowRadius
        }
    }
}