Drawing view on iOS
How can I implement a view for users to draw in (specifically sign their name)? I would greatly prefer a library or sample code to implementing it myself.
开发者_开发百科Thanks
Drawing a curved line is just a series of filled circles or small straight lines. A kind of differentiation.
- Track user's previous & current touch points. (
UITouch
) - Just draw a line which connect the points. (Or draw any shape what you want, just fill the gap)
Basics
There are series of methods for tracking touches.
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:
These methods are on UIView
.(methods themselves are defined in UIResponder
which is superclass of it)
Override them. In most cases tracking touch point is not a problem.
And send setNeedsDisplay
message to UIView itself to make it invalidate it's graphics state so it will redraw itself soon.
Override drawRect
method of the UIView
to draw something (like black disk) you need with Quartz (CGContext
). You may store tracked touch points to draw each lines. Basically, you have to redraw all of touch trails.
Optimization
- You can cache drawn image as a bitmap (
CGImage
which can be retrievedUIImage
). So you need to draw just newly added touch points. - You can draw on bitmap directly by setting drawing context to the bitmap. And just display it via UIImage.
- You can use OpenGL ES texture drawing. This is fastest, but hardest. And requires totally different approach for performance gain.
More detailed informations and samples are on reference of methods/classes I mentioned. I attach a link: http://developer.apple.com/iphone/library/navigation/index.html#section=Topics&topic=Graphics%20%26amp%3B%20Animation
精彩评论