How to draw like this in drawRect?
iPhone developing problem:
I want to draw 开发者_Python百科a quadrangle with the four black corners (CGPoint) of quadrangle changeable.
I overrode the drawRect
method but it shows only rectangle instead of quadrangle. Is that mean drawRect
can only draw Rectangles with four CGPoints?
The original requirement is changing the shape of quadrangle by touching and moving any of the corner points. Can I solve this by override drawRect
? should I do something else?
thanks.
-drawRect:
is where you put your drawing code, and that code can draw just about anything you want. You are certainly not limited to rectangles. I'm not sure I follow the particular nature of your problem -- it would help a lot if you'd post some code. In general -drawRect:
is the right place to put your drawing code, and if your view isn't drawing the way you want it to it's very likely due to a problem in your code rather than a limitation of the -drawRect:
method.
specifically, your view's frame defines its origin{x,y}
, and size{width,height}
.
this is a simple rectangle.
as well, a rectangle is passed for drawing arguments.
to have your view represent a quadrilateral more complex than a rectangle, you must write some custom geometry conversions (unless a function already exists). there are many apis to ease this (e.g. NSBezierPath/UIBezierPath).
ultimately, you want a frame which is large enough to contain the entire quadrilateral. when requested to draw a portion of it, you determine what to draw an how to fill it.
since you want 4 movable points within the rect, you must define 4 points (e.g. CGPoint) -- one for each corner. make these ivars of your view's subclass. when they change, invalidate the view as is appropriate.
精彩评论