ios - 如何在 Swift 3 中为 UIView 提供底部阴影?

标签 ios swift swift3 uiview

enter image description here

我要这个

enter image description here

我的代码

let shadowPath = UIBezierPath(rect: shadowView.bounds).cgPath
shadowView.layer.shadowColor = UIColor.grey.cgColor
shadowView.layer.shadowOffset = CGSize(width: 0, height: 1)
shadowView.layer.shadowOpacity = 0.5
shadowView.layer.masksToBounds = false
shadowView.layer.shadowPath = shadowPath
shadowView.layer.shadowRadius = 5

我想在我的 UIVIew 中实现完全相同的阴影。当我用上面的代码运行我的应用程序时,它分布在 4 个面。注意:阴影两边较细,中间较粗。

最佳答案

尝试创建自定义路径:

let shadowPath = UIBezierPath()
shadowPath.move(to: CGPoint(x: someView.bounds.origin.x, y: someView.frame.size.height))
shadowPath.addLine(to: CGPoint(x: someView.bounds.width / 2, y: someView.bounds.height + 7.0))
shadowPath.addLine(to: CGPoint(x: someView.bounds.width, y: someView.bounds.height))
shadowPath.close()

shadowView.layer.shadowColor = UIColor.darkGray.cgColor
shadowView.layer.shadowOpacity = 1
shadowView.layer.masksToBounds = false
shadowView.layer.shadowPath = shadowPath.cgPath
shadowView.layer.shadowRadius = 5

这会给出类似的结果,请尝试自定义点位置和其他值以满足您的需要。

关于ios - 如何在 Swift 3 中为 UIView 提供底部阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46315286/

相关文章:

ios - 如何动态切换屏幕

ios - 使用 Core Motion 检测道路颠簸

ios - 使用 swift 3 在 UIView 上添加阴影

ios - 二元运算符 '+=' 不能应用于 Swift 3 中 UICollectionViewLayoutAttributes 类型的操作数

ios - iOS前台启动图像

ios - 无法为 FacebookSDK 4 构建模块 'FBSDKCoreKit'

ios - 事件应用程序中的应用程序内购买失败

swift - AVAudioPlayer 没有播放任何东西

ios - 我如何在 iOS 上找到自定义 UIActivity 的包标识符或应用程序包

ios - 带有全选选项的swift 3.0多项选择