Resizing a CAShape Layer with the touch point or UIPanGestureRecognizer

36 views Asked by At

I have drawn a CAShapeLayer using UIBezierPath with touch on the ImageView with that I'm able to draw of any shape of CAShapeLayer over the specified ImageView.

Image for reference that I'm able to draw the CAShapeLayer With touch:

image

My second objective is to pan/Move the CAShape Layer drawn which is also I have achieved by adding pan gesture recogniser to the image view and by that able to translate the position of CAShape Layer subjected to the Image View Layer.

Reference for the CAShape Layer Translated using UIPanGesture Recogniser

enter image description here

Now I'm trying to resize the CAShape Layer with the same pan Gesture recogniser but for this time it get's disappeared as the translation value is too bigger/smaller to my assumption.

I have tried to translate the CAShape Position and to achieve it.

func addPanGesture(to view: UIImageView) {
        let panGesture                              = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
        self.displayImageView.addGestureRecognizer(panGesture)
        print("Added pan to Selected Polygon Shape")
        polygonSelectedShape.panGetureFinishedBool  = true
    }
    
    @objc func handlePanGesture(_ gestureRecognizer: UIPanGestureRecognizer) {
        guard polygonSelectedShape.panGetureFinishedBool else {return} //Annotation Shape Class File (PolygonSelectedShape)
        switch gestureRecognizer.state{
        case .began:
            print("Pan Gesture began : ", polygonSelectedShape.path as Any)
        case .possible:
            print("Pan Gesture began : ", polygonSelectedShape.path as Any)
        case .changed:
            let translation                  = gestureRecognizer.translation(in: displayImageView)
            let originalPath                 = polygonSelectedShape.path
            var polyTranslation              = CGAffineTransform(translationX: translation.x, y: translation.y)
            let translatePolyPath            = originalPath?.copy(using: &polyTranslation)
            polygonSelectedShape.position    = CGPoint(x: polygonSelectedShape.position.x + translation.x, y: polygonSelectedShape.position.y + translation.y)
            gestureRecognizer.setTranslation(.zero, in: displayImageView)
            polygonSelectedShape.path        = translatePolyPath
            polyCAShapeDictionary.updateValue(polygonSelectedShape, forKey: polySelectedShapekey)
            polygonSavedShape                = polygonSelectedShape
        case .ended:
            print("Ended : - Pan Gesture")
            polygonSelectedShape.panGestureBool        = false
            (panButton.backgroundColor, resizeButton.backgroundColor) = (.clear, .clear)
            self.removePanGesture(to: displayImageView)
            polygonSelectedShape.panGetureFinishedBool = false
            polygonSavedShape                          = polygonSelectedShape
            polyCAShapeDictionary.updateValue(polygonSelectedShape, forKey: polySelectedShapekey)
        case .cancelled:
            print("Cancelled - Pan Gesture")
        case .failed:
            print("Failed - Pan Gesture")
        @unknown default:
            break
        }
    }

The above code is to translate from one position to another.

Please suggest a idea for resizing the drawn CAShape Layer with touch control on the Image View.

0

There are 0 answers