IOS:如何在 4 侧(上、右、下、左)为 UIView 制作阴影

作者:编程家 分类: ios 时间:2025-08-06

在 iOS 开发中,我们经常需要给 UIView 添加阴影效果来提升界面的美观性和层次感。通常情况下,我们可以通过设置视图的 layer 属性来实现阴影效果。本文将介绍如何在 UIView 的四个边(上、右、下、左)添加阴影,并提供相应的代码示例。

首先,我们需要创建一个 UIView,然后设置其阴影效果。下面是一个简单的示例代码:

swift

let shadowView = UIView(frame: CGRect(x: 50, y: 50, width: 200, height: 200))

shadowView.backgroundColor = UIColor.white

// 设置阴影颜色和透明度

shadowView.layer.shadowColor = UIColor.black.cgColor

shadowView.layer.shadowOpacity = 0.5

// 设置阴影偏移量和模糊半径

shadowView.layer.shadowOffset = CGSize(width: 2, height: 2)

shadowView.layer.shadowRadius = 4

self.view.addSubview(shadowView)

上述代码中,我们创建了一个大小为 200x200 的白色 UIView,并设置了其阴影效果。通过设置 layer 的 shadowColor 属性,我们可以指定阴影的颜色。通过设置 shadowOpacity 属性,我们可以控制阴影的透明度。通过设置 shadowOffset 属性,我们可以调整阴影的偏移量。通过设置 shadowRadius 属性,我们可以控制阴影的模糊半径。

接下来,我们将详细介绍如何在 UIView 的四个边上添加阴影效果。

在上方添加阴影

要在 UIView 的上方添加阴影,我们只需要调整 shadowOffset 的高度值为负数即可。下面是相应的代码示例:

swift

let topShadowView = UIView(frame: CGRect(x: 50, y: 300, width: 200, height: 200))

topShadowView.backgroundColor = UIColor.white

topShadowView.layer.shadowColor = UIColor.black.cgColor

topShadowView.layer.shadowOpacity = 0.5

topShadowView.layer.shadowOffset = CGSize(width: 0, height: -2)

topShadowView.layer.shadowRadius = 4

self.view.addSubview(topShadowView)

在上述代码中,我们将 shadowOffset 的高度值设置为 -2,即阴影向上偏移 2 个点,从而实现在 UIView 的上方添加阴影效果。

在右侧添加阴影

要在 UIView 的右侧添加阴影,我们只需要调整 shadowOffset 的宽度值为正数即可。下面是相应的代码示例:

swift

let rightShadowView = UIView(frame: CGRect(x: 300, y: 50, width: 200, height: 200))

rightShadowView.backgroundColor = UIColor.white

rightShadowView.layer.shadowColor = UIColor.black.cgColor

rightShadowView.layer.shadowOpacity = 0.5

rightShadowView.layer.shadowOffset = CGSize(width: 2, height: 0)

rightShadowView.layer.shadowRadius = 4

self.view.addSubview(rightShadowView)

在上述代码中,我们将 shadowOffset 的宽度值设置为 2,即阴影向右偏移 2 个点,从而实现在 UIView 的右侧添加阴影效果。

在下方添加阴影

要在 UIView 的下方添加阴影,我们只需要调整 shadowOffset 的高度值为正数即可。下面是相应的代码示例:

swift

let bottomShadowView = UIView(frame: CGRect(x: 50, y: 550, width: 200, height: 200))

bottomShadowView.backgroundColor = UIColor.white

bottomShadowView.layer.shadowColor = UIColor.black.cgColor

bottomShadowView.layer.shadowOpacity = 0.5

bottomShadowView.layer.shadowOffset = CGSize(width: 0, height: 2)

bottomShadowView.layer.shadowRadius = 4

self.view.addSubview(bottomShadowView)

在上述代码中,我们将 shadowOffset 的高度值设置为 2,即阴影向下偏移 2 个点,从而实现在 UIView 的下方添加阴影效果。

在左侧添加阴影

要在 UIView 的左侧添加阴影,我们只需要调整 shadowOffset 的宽度值为负数即可。下面是相应的代码示例:

swift

let leftShadowView = UIView(frame: CGRect(x: 550, y: 50, width: 200, height: 200))

leftShadowView.backgroundColor = UIColor.white

leftShadowView.layer.shadowColor = UIColor.black.cgColor

leftShadowView.layer.shadowOpacity = 0.5

leftShadowView.layer.shadowOffset = CGSize(width: -2, height: 0)

leftShadowView.layer.shadowRadius = 4

self.view.addSubview(leftShadowView)

在上述代码中,我们将 shadowOffset 的宽度值设置为 -2,即阴影向左偏移 2 个点,从而实现在 UIView 的左侧添加阴影效果。

通过以上示例代码,我们可以在 UIView 的四个边(上、右、下、左)添加阴影效果。根据实际需求,我们可以调整阴影的颜色、透明度、偏移量和模糊半径,从而实现不同的阴影效果。希望本文对你在 iOS 开发中添加阴影效果有所帮助!