Search code examples
iosswiftcocoa-touchtouch-eventpdfview

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


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)

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


Solution

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