Draw diagonal line based on touch - ios

In Xcode 5, I am trying to make an app that allows the user to draw a line based on touch. I would have this working using UIBezierpath but then I am not able to remove the last line once the user tries to draw a new one. What method would I use to draw a line between touchDown and touchUp that can be easily deleted? Thanks in advance!

The easiest way is to use the UIBezierPath to construct a path which you then assign to a CAShapeLayer. You then add that layer as a sublayer to some view you already have in the interface. To delete the line, remove the layer.

Related

How to draw an editable line in iOS app?

I am developing an app which enable users to draw a connection between different components, I tried to use CGPath to draw a line. But the problem is, the line is not an object which I can not reference it later or edit it.
Since I want to enable the app to create a line when two points in screen is touched, and can delete it if need by touching the line, do you have any good ideas to implement this?
Thanks very much!
I think you're looking for CAShapeLayer. You can change its path property after it's been added to the layer hierarchy and it will update accordingly.

Swift: Drawing a UIBezierPath based on touch in a UIView

I've been looking at this thread as I'm trying to implement the same thing. However, I see that the Canvas class is implemented as a subclass of UIImageView. I'm trying to do the same thing except in a UIView. How will using a UIView rather than UIImageView affect the implementation of this solution? I see self.image used a couple times, but I don't know how I'd change that since I don't think that is available in a generic UIView.
Yes, you can implement this as a UIView subclass. Your model should hold the locations of the touch events (or the paths constructed from those locations) and then the drawRect of the view can render these paths. Or you create CAShapeLayer objects associated with those paths, too. Both approaches work fine.
Note, there is some merit to the approach of making snapshots (saved as UIImage) objects that you either show in a UIImageView or manually draw in drawRect of your UIView subclass. As your drawings get more and more complicated, you'll start to suffer performance issues if your drawRect has to redraw all of path segments (it can become thousands of locations surprisingly quickly because there are a lot of touches associated with a single screen gesture) upon every touch.
IMHO, I think that other answer you reference goes too far, making a new snapshot upon every touchesMoved. When you look at full resolution image for retina iPad or iPhone 6 plus, that's a large image snapshot to create upon every touch event. I personally adopt a hybrid approach: My drawRect or CAShapeLayer will render the current path associated with the current gesture (or the collection of touchesMoved events between touchesBegan and touchesEnded), but when the gesture finishes, it will create a new snapshot.
In the answer to that question, self.image is drawn into the drawing context first, then drawing is applied on top, then finally the image is updated to be the old image with new content drawn on top.
Since you just want to add a UIBezierPath, I'd just create a CAShapeLayer into which you place your bezier path, and place it on top of your views backing layer (self.view.layer). There's no need to do anything with DrawRect.

Draw and Erase on ImageView using UIBezierPath

I write this link's answer code to create ImageViewLayer and Imageview.
StackOverflow Link
This link gives me the below result.
It's work fine .But I want ,when user press the original button... and then touches with finger it remove the clear area ang fill with the previous one.
And how to create Undo Functionality for this..If any question regarding this please add your comment..Please help me...
how to draw CALayer with line path like simulate eraser effect?
this is the post i get the answer. there is a sample code about the effect.
what you are different is change the gray color layer to be the "paper imager"
hope it help you

How to remove last draw UIBezierPath from view?

I am drawing the line on touch using "UIBezierPath" UIBezierPath.
I want to implement the UNDO feature in which when user press on UNDO button then last drawn line should be removed from the screen.
How can i do this? I have used "removeAllPoints" function of "UIBezierPath" but it doesn't work.

Intercepting touches on MKOverlay border

One of the functions of program is to select a piece of the map. I do this using MKAnnotations and using a MKPolygonView (with just the border visible) to connect the "dots". (Please take a look at the screenshot below).
However, I'm trying to find a mechanism so that users can add new pins. This should be done by pressing on a border part of the MKPolygonView and then a new pin is added in the middle of the border.
In order to do this, I have to intercept touches, probably using the UIGestureRecognizer. I have looked at Touch events on MKMapView's overlays, which gave me a good lead. The only problem is that this intercepts touches also inside the MKPolygonView. I just need the border.
Is there any way to achieve this kind of behavior?
This is an old question, but anyway - one of the possible workarounds is using MKPolyline simultaneously. You could add a MKPolyline, matching MKPolygon border and detect taps on MKPolyline.

Resources