如何使用Beizer Path

时间:2018-03-31 09:32:57

标签: ios swift uitableview shadow

我在没有给出底部阴影的视图的3个边上添加了阴影,但是在初始加载时,它向所有角落显示阴影,但是当我滚动它时显示正确的结果。这是我的代码。

class ScheduleClassesTableViewCell:UITableViewCell {

let path = UIBezierPath()

//MARK:-
//MARK:- IBOUTLETS

@IBOutlet weak var cellImage: UIImageView!
@IBOutlet weak var openTime: UILabel!
@IBOutlet weak var closeTime: UILabel!
@IBOutlet weak var clockImage: UIImageView!
@IBOutlet weak var instructerNameLabel: UILabel!
@IBOutlet weak var classTypeLabel: UILabel!
@IBOutlet weak var healthClubLocationLabel: UILabel!
@IBOutlet weak var ratingsBtn: UIButton!
@IBOutlet weak var locationBtn: UIButton!
@IBOutlet weak var healthClubName: UILabel!
@IBOutlet weak var healthClubLoc: UILabel!
@IBOutlet weak var shadowView: UIView!

//MARK:-
//MARK:- TableView Life Cycle

override func awakeFromNib() {
    super.awakeFromNib()

    self.healthClubLoc.isHidden = false
    self.ratingsBtn.doCorner(cornerRadius: 2.5)
    self.locationBtn.doCorner(cornerRadius: 2.5)
    self.ratingsBtn.setBorder(with: UIColor.gray, of: 0.5)
    self.locationBtn.setBorder(with: UIColor.white, of: 1.0)
    self.cellImage.doCorner(cornerRadius: 2.5)
}

override func draw(_ rect: CGRect) {
    super.draw(rect)
    self.shadowView.layoutIfNeeded()
    path.move(to: CGPoint(x: 0.0, y: shadowView.frame.size.height))
    path.addLine(to: CGPoint(x: 0.0, y: 0.0))
    path.addLine(to: CGPoint(x: shadowView.frame.size.width, y: 0.0))
    path.addLine(to: CGPoint(x: shadowView.frame.size.width, y: shadowView.frame.size.height))
    self.shadowView.drawShadow(shadowColor: UIColor.red, shadowOpacity: 0.8, shadowPath: path, shadowRadius: 2, cornerRadius: 0.0)

}

before scrolling

before scrolling

after scrolling

after scrolling

1 个答案:

答案 0 :(得分:1)

override func draw(_ rect: CGRect) {
    super.draw(rect)

    path.move(to: CGPoint(x: 0.0, y: shadowView.frame.size.height))
    path.addLine(to: CGPoint(x: 0.0, y: 0.0))
    path.addLine(to: CGPoint(x: shadowView.frame.size.width, y: 0.0))
    path.addLine(to: CGPoint(x: shadowView.frame.size.width, y: shadowView.frame.size.height))
    self.shadowView.drawShadow(shadowColor: UIColor.red, shadowOpacity: 0.8, shadowPath: path, shadowRadius: 2, cornerRadius: 0.0)
    layoutIfNeeded()

}

请更新您的代码