Tuesday, August 16, 2022
HomeiOS Developmentios - The distinction between UITableView HeaderView and UITableViewCell when utilizing UIBezierPath

ios – The distinction between UITableView HeaderView and UITableViewCell when utilizing UIBezierPath


enter picture description right here

class CornerCell: UITableViewCell {

@IBOutlet weak var exLabel: UILabel!

non-public var borderLayer = CAShapeLayer()
non-public var myType: RoundedTableViewCellType = .center

override var body: CGRect {
    get {
        return tremendous.body
    }
    set {
        let inset: CGFloat = 20
        var body = newValue
        body.origin.x += inset
        body.dimension.width -= 2 * inset
        tremendous.body = body
    }
}

var borderColor: UIColor = .clear {
    didSet {
        borderLayer.strokeColor = borderColor.cgColor
    }
}

var borderWidth: CGFloat = 0.0 {
    didSet {
        borderLayer.lineWidth = borderWidth
    }
}

var radius: CGFloat = 6.0 {
    didSet {
        kind = myType
    }
}

var kind: RoundedTableViewCellType = .center {
    didSet {
        myType = kind
        swap kind {
        case .first:
            layer.cornerRadius = radius
            layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        case .final:
            layer.cornerRadius = radius
            layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
        case .single:
            layer.cornerRadius = radius
            layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner, .layerMinXMinYCorner, .layerMaxXMinYCorner]
        case .center:
            layer.cornerRadius = 0
            layer.maskedCorners = []
        }
    }
}

override func layoutSubviews() {
    tremendous.layoutSubviews()
    
    let r = radius
    var bPath = UIBezierPath()
    
    let ptTopLeft = CGPoint(x: 0.0, y: 0.0)
    let ptTopRight = CGPoint(x: bounds.width, y: 0.0)
    let ptBotRight = CGPoint(x: bounds.width, y: bounds.top)
    let ptBotLeft = CGPoint(x: 0.0, y: bounds.top)
    
    swap kind {
    case .first:
        bPath.transfer(to: ptBotLeft)
        bPath.addLine(to: CGPoint(x: ptTopLeft.x, y: ptTopLeft.y + r))

        bPath.addQuadCurve(to: CGPoint(x: ptTopLeft.x + r, y: ptTopLeft.y),
                           controlPoint: ptTopLeft)

        bPath.addLine(to: CGPoint(x: ptTopRight.x - r, y: ptTopRight.y))

        bPath.addQuadCurve(to: CGPoint(x: ptTopRight.x, y: ptTopRight.y + r),
                           controlPoint: ptTopRight)

        bPath.addLine(to: CGPoint(x: ptBotRight.x, y: ptBotRight.y))

    case .final:
        bPath.transfer(to: ptTopLeft)
        
        bPath.addLine(to: CGPoint(x: ptBotLeft.x, y: ptBotLeft.y - r))
        bPath.addQuadCurve(to: CGPoint(x: ptBotLeft.x + r, y: ptBotLeft.y), controlPoint: ptBotLeft)
        
        bPath.addLine(to: CGPoint(x: ptBotRight.x - r, y: ptBotRight.y))
        bPath.addQuadCurve(to: CGPoint(x: ptBotRight.x, y: ptBotRight.y - r), controlPoint: ptBotRight)
        
        bPath.addLine(to: CGPoint(x: ptTopRight.x, y: ptTopRight.y))
    case .single:
        bPath = UIBezierPath(roundedRect: bounds, cornerRadius: r)

    case .center:
        bPath.transfer(to: ptTopLeft)
        bPath.addLine(to: ptBotLeft)
        bPath.addLine(to: ptBotRight)
        bPath.addLine(to: ptTopRight)
    }
    
    borderLayer.path = bPath.cgPath
}

override func awakeFromNib() {
    tremendous.awakeFromNib()
    
    commonInit()
}

func commonInit() {
    layer.addSublayer(borderLayer)

    borderLayer.fillColor = UIColor.clear.cgColor

    // default values
    borderColor = .black
    borderWidth = 1.0
}

}

class CustomHeaderView: UITableViewHeaderFooterView {

@IBOutlet weak var titleLabel: UILabel!

@IBOutlet weak var plusButton: UIButton!

non-public var borderLayer = CAShapeLayer()
non-public var myType: RoundedTableViewCellType = .center

override var body: CGRect {
    get {
        return tremendous.body
    }
    set {
        let inset: CGFloat = 20
        var body = newValue
        body.origin.x += inset
        body.dimension.width -= 2 * inset
        tremendous.body = body
    }
}

var borderColor: UIColor = .clear {
    didSet {
        borderLayer.strokeColor = borderColor.cgColor
    }
}

var borderWidth: CGFloat = 0.0 {
    didSet {
        borderLayer.lineWidth = borderWidth
    }
}

var radius: CGFloat = 6.0 {
    didSet {
        kind = myType
    }
}

var kind: RoundedTableViewCellType = .center {
    didSet {
        myType = kind
        swap kind {
        case .first:
            layer.cornerRadius = radius
            layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        case .final:
            layer.cornerRadius = radius
            layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
        case .single:
            layer.cornerRadius = radius
            layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner, .layerMinXMinYCorner, .layerMaxXMinYCorner]
        case .center:
            layer.cornerRadius = 0
            layer.maskedCorners = []
        }
    }
}

override func layoutSubviews() {
    tremendous.layoutSubviews()
    
    let r = radius
    var bPath = UIBezierPath()
    
    let ptTopLeft = CGPoint(x: 0.0, y: 0.0)
    let ptTopRight = CGPoint(x: bounds.width, y: 0.0)
    let ptBotRight = CGPoint(x: bounds.width, y: bounds.top)
    let ptBotLeft = CGPoint(x: 0.0, y: bounds.top)
    
    swap kind {
    case .first:
        bPath.transfer(to: ptBotLeft)
        bPath.addLine(to: CGPoint(x: ptTopLeft.x, y: ptTopLeft.y + r))

        bPath.addQuadCurve(to: CGPoint(x: ptTopLeft.x + r, y: ptTopLeft.y),
                           controlPoint: ptTopLeft)

        bPath.addLine(to: CGPoint(x: ptTopRight.x - r, y: ptTopRight.y))

        bPath.addQuadCurve(to: CGPoint(x: ptTopRight.x, y: ptTopRight.y + r),
                           controlPoint: ptTopRight)

        bPath.addLine(to: CGPoint(x: ptBotRight.x, y: ptBotRight.y))

    case .final:
        bPath = UIBezierPath(roundedRect: bounds,
                             byRoundingCorners: [.bottomLeft, .bottomRight],
                             cornerRadii: CGSize(width: r, top: r))

    case .single:
        bPath = UIBezierPath(roundedRect: bounds, cornerRadius: r)

    case .center:
        bPath.transfer(to: ptTopLeft)
        bPath.addLine(to: ptBotLeft)
        bPath.addLine(to: ptBotRight)
        bPath.addLine(to: ptTopRight)
    }
    
    borderLayer.path = bPath.cgPath
}

override func awakeFromNib() {
    tremendous.awakeFromNib()
    
    commonInit()
}

func commonInit() {
    layer.addSublayer(borderLayer)

    borderLayer.fillColor = UIColor.clear.cgColor

    // default values
    borderColor = .black
    borderWidth = 1.0
}

}

I used identical code, however headerView borderWidth is extra thick than cell.
( headerView’s kind is ‘first’ & cell’s kind is ‘center’and ‘final’ )
I do not know why this outcome was.

I wish to draw identical borderWidth.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular