how to draw above cashapelayer path

202 Views Asked by At

I am having a CAShapeLayer i want to draw above that layer.. I have taken a UIImageView and added on that CAShapelayer and now i starting drawing as user touch moves inside that CAShapelayer path. But when user touch ends i want to merge that drawn image view on CAShape layer in such a way that if users taps again then using hitTest method i can again get the path of cashape layer.

// tempDrawingImage is the image view that i added above hit layer.

UIGraphicsBeginImageContext(tempDrawingImage.frame.size);
        [hitLayer drawInContext:UIGraphicsGetCurrentContext()];
        [tempDrawingImage.image drawInRect:tempDrawingImage.frame];
        hitLayer.contents = (id) UIGraphicsGetImageFromCurrentImageContext().CGImage;
        UIGraphicsEndImageContext();
1

There are 1 best solutions below

0
On

You cannot easily convert an image to a path for the CAShapeLayer.

You need to have a drawingPath which will be modified according to the touch events. Also a second CAShapeLayer will be needed to draw the current drawingPath (since you will get the same results while drawing as after the merge which is not guaranteed using the image approach). When merging you need to append the drawingPath to the path of your shapeLayer.