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:].
Related
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
My question might be answered somewhere but I just couldn't find solution after a long research.
I need to draw visually shapes (curves, lines, rectangles etc.) on iPhone one on top of the previous. I did that using CGContext to draw over an image and it's working fine. However in my app the drawing view resizes on device rotation and the lines become blurred because of the different image size.
That's why I subclassed UIView and to call setNeedsDisplay from touchesMoved and touchesEnded. In drawRect I'm passing the point and... almost everything works OK.
However I have two problems:
1. Every time drawRect is called it clears previous drawing and starts over so I can't add a new shape.
2. Second is followed by same thing - I can't make a curve as on every move drawRect is called and the previous point is dismissed and a line is added from the starting point to the current.
So am I doing the whole thing wrong and is there some other better approach to this.
Thanks in advance!
Is it possible to animate something that you have drawn using core graphics in a UIView's drawRect method?
Say for example I have created a zigzag line by stroking a path. What if I want to animate something like the position of an individual point in that path? If my UIView has the properties animatedPointX and animatedPointY which are used to draw my point, can I somehow animate my zigzag line by changing these properties? Is something like this possible with Core Graphics?
Every animation example and tutorial I have found deals with animating an entire view as a whole. I cant seem to find anything about animating custom properties of your drawing/layer.
If you want to animate the graphics that you draw in drawRect:, you need to periodically update the variables that control what you draw and send setNeedsDisplay to the view.
Apple recommends using a CADisplayLink object to periodically notify yourself that it's time to update and redraw.
You can see an example of the use of CADisplayLink to trigger the update and setNeedsDisplay in the ARView.m file of the pARk sample app.
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!
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