简体   繁体   中英

How to draw like this in drawRect?

iPhone developing problem: I want to draw 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 (eg 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 (eg CGPoint) -- one for each corner. make these ivars of your view's subclass. when they change, invalidate the view as is appropriate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM