I just want to draw point and line as per finger touch and moved in iphone screen.
What is the best way to draw point in iphone?
Can anyboday tell me how to draw point as per finger touch and moved in iphone ?
Thanks,
You can't "just draw" on the screen. Closest approximation might be to save some coordinates from some view's touch delegate methods and call setNeedsDisplay. When the view's drawRect is called, you can then use some Core Graphics (CG) drawing commands with those saved coordinates into the given drawing context. The OS will shortly then composite that view context onto the display screen.
check with this tutorial,i am sure it will definetly help you.
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_freehand-drawing/
you can draw line on Image or on UIView using UIBezierPath class.. in this class object you can store the user touch points and draw line using this points array..
for more details see the tutorial and demo from this link..
Drawing on View and Image
Related
I know that you can draw a line easy by Core Graphic on UIView in a drawrect function. However, I only can draw a straight line。 If I want to draw a arbitrary path with the touch move, what should I do? Can someone give a hint?
Best Regards,
You could use the CGPoints that are being passed to touchesMoved: and draw a line in between each on of those CGPoints in order to draw your arbitrary path. You would only need to draw a new line once, unless you are redrawing your screen, which means you would have to store all the points in an NSMutableArray, and redraw them every time. Hope that Helps!
is it possible to draw Ellipse through UITouchMoved in iphone sdk as we do in MSPaint?Any
example please?
Just save the rect in the touch moved delegate, and draw an ellipse in the rect (maybe using CGContextStrokeEllipseInRect) whenever the view's drawRect gets called. Call setNeedsDisplay to say that the drawRect needs to be called sometime before returning to the run loop.
Guessing that you mean ellipse rather than eclipse? If so, use +[UIBezierPath bezierPathWithOvalInRect:].
How can pixels be drawn at a touch location?
If the user touches a certain area of a view, how are the locations used to draw/mark the location with e.g black color?
What kind of view is this or how is this drawing done?
Generally, if you're using Quartz you'll:
handle your touch events
store the points that are streaming in in some datastructure
invalidate your view so it draws
override drawRect and draw the points you stored in 2.
UIBezierPath is a class that can help with this. You can essentially "draw into" an instance of UIBezierPath when you receive the touch events, then render the object in drawRect.
Anyone who has gone through the Stanford CS193P class on iTunes will recognize this from Assignment 3 (HelloPoly). For those not familiar:
I have a custom view called polyView, an instance of PolygonView, a subclass of UIView. On this view, I use CGContextDrawLinearGradient to paint a color gradient over the entire rectangular view. Then I use CGContextDrawPath to stroke and fill a polygon within the bounds of polyView. And I have a UILabel called nameLabel in the center of the view (and polygon) that displays the name of the polygon (triangle, quadrilateral, pentagon, etc.). All of this works fine, and the code to do all this is in the -(void)drawRect method of the PolygonView class.
Where I ran into trouble is with an additional requirement to rotate the polygon within the view in response to user gestures. I used CGAffineTransformRotate() in response to touchesBegan() and touchesMoved() events within the PolygonView class, and this basically works, too. But I can only rotate the entire polyView, not just the polygon drawn on it. I'm sure I could go back and recalculate the path of the polygon and redraw/fill the path in response to each touchesMoved() event, but that would be expensive and can't be the best method. How can I use CGAffineTransformRotate to rotate just the polygon, without rotating the gradient-filled view or the label in the center?
Or is there some way to create the polygon on a layer that I can place over the background polyView at the desired rotation angle?
Thanks for any help you can give a beginner here!
Duane
You can do a CGContextSaveGState(...) just before doing the rotation transformation, then drawing the polygon and restoring the drawing state with a CGContextRestoreGState(...) afterwards, so as not to affect any other drawing in the view later.
I'm writing an application that uses your finger to draw simple diagrams. I have it working for the most part but now I'm trying to optimize its performance. When the user swipes their finger fast, I can't capture enough touch events to draw a smooth path.
Here's my current approach:
1) I subclassed a UIView and added a poroperty to a CGLayer (gets created lazily and is the same size as my UIView).
2) My UIView subclass responds to touch events by storing the current and last touch points in instance variables.
3) My view's setNeedsDisplay is called and in the draw rect , do the following:
- draw a line from the previous touch location to the current touch location to the CGLayer
- draw the entire CGLayer to my views context in one go
The main problem is when a user swipes fast I get relatively few touch events, so the lines I draw between the touches are long and makes the path look jagged not smooth.
My questions:
1) Does drawRect (on my UIView subclass) and my touch event handlers on my UIView subclass get called in the same thread? I.e. could I have to threads executing (one in a touch event and the second in my draw rect)?
If no - do touch events get queued up while drawRect is being called? And how can I improve performance - simply improve performance of drawRect?
If yes - how can I get more touch events to happen so I can draw a smoother path?
Thanks.
Another approach is to interpolate the curve between the sample points. When the finger drag starts, begin collecting sample points. As the number of points increase, redraw the line. With two points, draw a straight line, with three or more draw a curve. You can re-start the process when two points are sampled that lie within a defined distance. This would allow you to draw two arcs (like a 'm') in one motion - you naturally pause in the middle as you change direction, possibly long enough for two or more samples.
drawRect gets called on the main thread. But you don't have to do this. You can use the main thread to collect UI events and do the drawing on a background thread. The background thread gets notified whenever there are new touches and starts a drawing operation in its own CGBitmapContext. Then you create a CGImage and hand it over to the View: view.layer.contents = drawingImage.
If you need even more performance, consider drawing using OpenGL.
Aloo, did you have find a solution to his as I've got the same problem. I also found agreat tutorial http://www.ipodtouchfans.com/forums/showthread.php?t=132024 but it also has the same problem that if you draw fast, say a circle, the drawing isn't very smooth. It;s almost like the iPhone just can't keep up, unfortunately this has to use the core graphics stuff.
Have you tried this?
http://developer.apple.com/library/ios/#samplecode/GLPaint/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007328-Intro-DontLinkElementID_2
I tried adding
CGContextSetLineJoin(UIGraphicsGetCurrentContext(), kCGLineJoinRound);
but this did nothing. Looks like we'll have to figure out bezier curves