2 Replies
      Latest reply on Feb 2, 2019 1:45 AM by Claude31
      BurierA Level 1 Level 1 (0 points)

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

        • Re: how to convert UIView inside UITableViewCell to half disc in swift 4.2?
          Claude31 Level 8 Level 8 (5,485 points)

          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

            • Re: how to convert UIView inside UITableViewCell to half disc in swift 4.2?
              Claude31 Level 8 Level 8 (5,485 points)

              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