在 PDFView 上 moving/dragging 时如何限制位置视图? (Swift)

How I can restrict position view while moving/dragging on PDFView? (Swift)

我在 pdf 上使用了一个自定义视图 (SignatoryView),当我从导航栏中单击“添加”按钮时,它将在 PDF 视图上添加该 SignatoryView,并且根据我的选择,我可以 move/drag 该签名视图到任何位置。

问题:当我在 pdf 上移动签名视图时,它超出了 pdfView 的边缘。 (还有左、右、下、上)

它不应超出其边界,它应该只能在 PDF 视图的内部边缘移动。

我怎样才能做到这一点? Here is the complete project code

你只需要得到签名宽度的一半和高度的一半,并在设置新的中心位置时从原点加上或减去它 x and/or y:

let minX = frame!.width/2
let minY = frame!.height/2
let maxX = pdfView.frame.width-minX
let maxY = pdfView.frame.height-minY
customView1?.center = CGPoint(
    x: min(maxX, max(minX, touchLocation!.x)),
    y: min(maxY ,max(minY, touchLocation!.y)))