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 :
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)