gpt4 book ai didi

ios - 如何只在 UITableView 的底部添加阴影?

转载 作者:搜寻专家 更新时间:2023-11-01 05:48:39 26 4
gpt4 key购买 nike

我有一个分段的 UITableView,它的标题高度为 10,它的 backgroundColorclearColor,只是为了显示更分离的部分。现在我只需要在每个部分的底部添加一个阴影。我尝试在 viewForHeaderInSection 方法中添加阴影,但结果很糟糕。实现此目标有什么技巧吗?

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let shadowView = UIView()

shadowView.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).CGColor
shadowView.layer.shadowOffset = CGSizeMake(0.0, 2.0)
shadowView.layer.shadowOpacity = 1.0
shadowView.layer.shadowRadius = 0.0
shadowView.layer.masksToBounds = false

return shadowView
}

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 2
}

最佳答案

我制作了这两个示例 Swift 3。第一个是经典的阴影,我使用了 CAGradientLayer。第二个是阴影弧,我使用了 UIBezierPathCAShapeLayerCAGradientLayer


经典阴影:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let shadowView = UIView()




let gradient = CAGradientLayer()
gradient.frame.size = CGSize(width: myTab.bounds.width, height: 15)
let stopColor = UIColor.gray.cgColor

let startColor = UIColor.white.cgColor


gradient.colors = [stopColor,startColor]


gradient.locations = [0.0,0.8]

shadowView.layer.addSublayer(gradient)


return shadowView
}

这是结果:

enter image description here

阴影弧:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let shadowView = UIView()

let path = UIBezierPath()
path.move(to: CGPoint.zero)
path.addLine(to: (CGPoint(x: (myTab.bounds.maxX), y: 0.0)))
path.addLine(to: (CGPoint(x: (myTab.bounds.maxX), y: 15)))
path.addCurve(to: (CGPoint(x: 0.0, y: 15)), controlPoint1: (CGPoint(x: (myTab.bounds.midX), y: -10)), controlPoint2: (CGPoint(x: 0.0, y: 15)))
path.addLine(to: CGPoint.zero)



let shape = CAShapeLayer()
shape.path = path.cgPath

let gradient = CAGradientLayer()

gradient.frame.size = CGSize(width: myTab.bounds.width, height: 15)
let stopColor = UIColor.gray.cgColor

let startColor = UIColor.white.cgColor


gradient.colors = [stopColor,startColor]


gradient.locations = [0.0,0.9]

gradient.mask = shape

shadowView.backgroundColor = UIColor.white
shadowView.layer.addSublayer(gradient)




return shadowView


}

这是结果:

enter image description here

关于ios - 如何只在 UITableView 的底部添加阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40026892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com