为什么我的表视图阴影滚动我的表视图?

时间:2016-12-20 10:13:01

标签: ios swift uitableview

我在表格视图中添加了阴影,但不幸的是,当我滚动表格视图时,阴影也随表格一起移动。添加阴影的代码如下:

func addShadow(to myView: UIView){
        let shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: uiView.frame.width, height: uiView.frame.height * 1.1))
        uiView.layer.shadowColor = UIColor.black.cgColor
        uiView.layer.shadowOffset = CGSize(width: 0.2, height: 0)
        uiView.layer.shadowOpacity = 0.5
        uiView.layer.shadowRadius = 5.0
        uiView.layer.masksToBounds = false
        uiView.layer.shadowPath = shadowPath.cgPath
    }

enter image description here

您能否向我解释为什么会发生这种情况以及如何让影子留在指定地点?

提前谢谢。

2 个答案:

答案 0 :(得分:0)

如果要在tableView上添加阴影,它将与tableview数据一起滚动。为了防止你必须先添加UIView。在该视图上添加tableview。为你拍摄的UIView添加阴影。它将坚持到指定的位置。

答案 1 :(得分:0)

这是我在 Swift 3 中的解决方案,其中包含UIViewCAGradientLayer

override func viewWillAppear(_ animated: Bool) {
      addShadow(myView: myTab)
 }

func addShadow(myView: UIView){        
    let shadowView = UIView()
    shadowView.center = CGPoint(x: myView.frame.minX,y:myView.frame.minY - 15)
    shadowView.frame.size = CGSize(width: myView.frame.width, height: 15)
    let gradient = CAGradientLayer()
    gradient.frame.size = shadowView.frame.size
    let stopColor = UIColor.clear.cgColor
    let startColor = UIColor.black.withAlphaComponent(0.8).cgColor
    gradient.colors = [stopColor,startColor]
    gradient.locations = [0.0,1.0]
    shadowView.layer.addSublayer(gradient)
    view.addSubview(shadowView)
}

enter image description here

相关问题