how to convert UIView inside UITableViewCell to half disc in swift 4.2?

how to convert UIView inside UITableViewCell to half disc in swift 4.2?

Accepted Reply

Do you want the view to draw as half disk ?


Then, create a subclass of UIView

In the draw method, draw the half disk using Bezier Path (set the right params)


class HalfDisk: UIView {
    override func draw(_ rect: CGRect) {       
        let center = CGPoint(x: rect.midX, y: rect.midY)
        let radius = rect.size.width / 2
        let startAngle : CGFloat = 0
        let endAngle = CGFloat.pi
        let halfDiskPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        UIColor.blue.setFill()
        halfDiskPath.fill()
    }
}


Give this class to the UIVIew you have created in UITableViewCell in IB

Replies

Do you want the view to draw as half disk ?


Then, create a subclass of UIView

In the draw method, draw the half disk using Bezier Path (set the right params)


class HalfDisk: UIView {
    override func draw(_ rect: CGRect) {       
        let center = CGPoint(x: rect.midX, y: rect.midY)
        let radius = rect.size.width / 2
        let startAngle : CGFloat = 0
        let endAngle = CGFloat.pi
        let halfDiskPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        UIColor.blue.setFill()
        halfDiskPath.fill()
    }
}


Give this class to the UIVIew you have created in UITableViewCell in IB

And if you want to set the color from IB, make it IBDesignable:


@IBDesignable
class HalfDisk: UIView {

    @IBInspectable var color: UIColor = .blue     // To have a default
   
    override func draw(_ rect: CGRect) {
       
        let center = CGPoint(x: rect.midX, y: rect.midY)
        let radius = rect.size.width / 2
        let startAngle : CGFloat = 0
        let endAngle = CGFloat.pi
        let halfDiskPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        color.setFill()
        halfDiskPath.fill()
    }
}

And to have an upperDisk, change with


        let startAngle : CGFloat = .pi // 0
        let endAngle : CGFloat = 0 // CGFloat.pi