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

六月ゝ 毕业季﹏ 提交于 2021-01-28 07:58:45

问题


I took one custom view (SignatoryView) on pdf, When I click on Add button from Navigation bar, it will add that SignatoryView on PDF view and according to my choice I can move/drag that signatory view to any location.

Problem: When I am moving that signatory view on pdf, it is going outside edges of pdfView. (Left, right, bottom and top also)

Here is the demo:

It should not go beyond its boundaries, it should be movable only inside edges on PDF view.

How I can achieve this ? Here is the complete project code


回答1:


You just need to get half of the width and half of the height of your signature and when setting the new center position of it add or subtract it from the origin 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)))


来源:https://stackoverflow.com/questions/64475554/how-i-can-restrict-position-view-while-moving-dragging-on-pdfview-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!