简体   繁体   English

如何在drawRect中这样绘制?

[英]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. iPhone开发中的问题:我想画一个四边形,四边形的四个黑角(CGPoint)可变。 I overrode the drawRect method but it shows only rectangle instead of quadrangle. 我覆盖了drawRect方法,但它仅显示矩形而不是四边形。 Is that mean drawRect can only draw Rectangles with four CGPoints? 这是否意味着drawRect只能绘制具有四个CGPoint的矩形?

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 ? 我可以通过覆盖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. -drawRect:是放置绘图代码的位置,该代码可以绘制几乎任何您想要的东西。 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. 通常, -drawRect:是放置绘图代码的正确位置,如果视图未按照您希望的方式进行绘图,则很可能是由于代码中的问题而不是-drawRect:方法的局限性。

specifically, your view's frame defines its origin{x,y} , and size{width,height} . 具体来说,您的视图框架定义了它的origin{x,y}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). 有许多API可以缓解这种情况(例如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. 由于要在矩形内有4个可移动点,因此必须定义4个点(例如CGPoint)-每个角一个。 make these ivars of your view's subclass. 使视图的子类成为这些错误。 when they change, invalidate the view as is appropriate. 当它们更改时,请视情况使视图无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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