简体   繁体   English

如何仅在UITableView部分的底部添加阴影?

[英]How to add shadow only at the bottom of section of UITableView?

I have a sectioned UITableView which has a header of height 10 and it has backgroundColor as clearColor just to show the sections more separated. 我有一个分段的UITableView ,其标题的高度为10,并且它的backgroundColor作为clearColor只是为了显示各部分之间的分隔。 Now I need to add a shadow only at the bottom of each section. 现在,我只需要在每个部分的底部添加阴影。 I've tried adding shadow to the viewForHeaderInSection method, but the result is very bad. 我尝试将阴影添加到viewForHeaderInSection方法中,但是结果非常糟糕。 Any tips for achieving this? 有什么技巧可以做到这一点吗?

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
}

I made these two example Swift 3 . 我举了两个Swift 3的例子。 The first it's a classic shadow, I used CAGradientLayer . 首先是经典阴影,我使用了CAGradientLayer The second it's a shadow arc, I used UIBezierPath , CAShapeLayer and CAGradientLayer . 第二个是阴影弧,我使用了UIBezierPathCAShapeLayerCAGradientLayer


Classic Shadow: 经典阴影:

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
}

This is the result: 结果如下:

在此处输入图片说明

Shadow Arc: 暗影之弧

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


}

This is the result: 结果如下:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM