我正在使用pdfkit并添加了具有固定大小和宽度的圆形注释,但是我想以动态的高度和宽度进行绘制。这是我的代码:
在这里:start是我的CGPoint,从这里开始 结束是第二个CGPoint,我结束了移动手指的操作。 使用了start.x和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)
这是绘制具有动态高度和宽度的圆的第二种方法:
这是代码:
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)
第二种方法绘制具有动态高度和宽度的圆,但不是我想要的。如果我画圆,他们是8种情况:
答案 0 :(得分:2)
您可以使用此代码在pdf页面上绘制圆圈
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)