如何在pdfkit swift中添加圆圈注释?

时间:2018-12-14 07:52:20

标签: swift3 annotations pdfkit wwdc pdf-annotations

我正在使用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种情况:

  1. 手指从左向右滑动-在适当的位置绘制圆圈。
  2. 手指从右向左滑动-在适当的位置绘制圆圈。
  3. 手指从左上方滑动到右下方-画出一半大小的圆圈
  4. 手指从右下滑动到左上-画圆的一半大小
  5. 手指从上到下滑动-圆半径值是宽度或高度的2或3
  6. 手指从下往上滑动-圆半径值为宽度或高度的2或3
  7. 手指从右上方滑动到左下方-画出一半大小的圆圈
  8. 手指从左下角向右上角滑动-画出一半大小的圆圈

1 个答案:

答案 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)