swift3annotationspdfkitwwdcpdf-annotations

How to add circle annotation in pdfkit swift?


I am using pdfkit and added circle annotation with fixed size and width but i want to draw with dynamic height and width. Here is my code :

Here : start is my CGPoint from where i start to finger end is second CGPoint where i ended to move finger. Used start.x and end.y

  let circle = PDFAnnotation(bounds: CGRect(x: start.x, y: end.y, width: 100, height: 100), forType: .circle, withProperties: nil)
  circle.color = hexStringToUIColor(hex: "#0000FF")

  let border = PDFBorder()
  border.lineWidth = 3.0
  circle.border = border
  page?.addAnnotation(circle)

This is second approach to draw circle with dynamic height and width:

Here is code :

    let centerX = (start!.x + end!.x)/2

    let centerY = (start!.y + end!.y)/2

    var distance = (end!.x - centerX) * 2

    if distance < 0 {

         distance = (start!.x - centerX) * 2

    }

    let halfDistance = distance/2

    self.annotation = PDFAnnotation(bounds: CGRect(x: centerX - halfDistance, y: centerY - halfDistance, width: distance, height: distance), forType: .circle, withProperties: nil)

    let page = self.pdfview.currentPage

    annotation.color = hexStringToUIColor(hex: "#0000FF")

    let border = PDFBorder()
    border.lineWidth = 3.0
    annotation.border = border
    page?.addAnnotation(annotation)

Second approach draws circle with dynamic height and width but not as i want. if i draw circle their are 8 cases :

  1. Finger swiped from left to right - It draw circle on proper position.
  2. Finger swiped from right to left - It draw circle on proper position.
  3. Finger swiped from top left to bottom right - It draw circle half of size
  4. Finger swiped from bottom right to top left - It draw circle half of size
  5. Finger swiped from top to bottom - circle radius value is 2 or 3 width and height
  6. Finger swiped from bottom to top - circle radius value is 2 or 3 width and height
  7. Finger swiped from top right to bottom left - It draw circle half of size
  8. Finger swiped from bottom left to top right - It draw circle half of size

Solution

  • You can use this code to draw circle over a pdfpage

       let size = CGSize(width: abs(point.x - startPoint.x), height: abs(point.y - startPoint.y))
            var rect = CGRect(origin: startPoint, size: size)
                   if point.y - startPoint.y < 0 && point.x - startPoint.x < 0
                    {
                        rect = CGRect(origin: point, size: size)
                    }
                    else if point.y - startPoint.y > 0 && point.x - startPoint.x < 0
                    {
                        rect = CGRect(origin: CGPoint(x: point.x, y: startPoint.y), size: size)
                    }
                    else if point.y - startPoint.y < 0 && point.x - startPoint.x > 0
                    {
                        rect = CGRect(origin: CGPoint(x: startPoint.x, y: point.y), size: size)
                    }
            let page = docView.currentPage
            let pageBounds = page!.bounds(for: .cropBox)
            let newAnnotation = PDFAnnotation(bounds: pageBounds, forType: .circle,withProperties:nil)
            newAnnotation.setRect(rect, forAnnotationKey: .rect)
            newAnnotation.color = UIColor.black
            page!.addAnnotation(newAnnotation)