有什么可能的方法从 UIViewcontroller 类调用 drawRect 吗?
我有一个名为 AppController.h
、AppController.m
的 UIViewController
类。我在那里有数千行代码,这是我在询问之前没有测试它的唯一原因。有没有可能在 UIViewController
中使用 drawRect
?这样我就不必创建更多的委托、方法和回调。我应该开始使用 drawRect
来处理我的绘图代码,但我没有,而且 iPad 上的核心图形存在严重的滞后。因此,请告诉我是否有任何方法可以在 UIViewController
中使用 drawRect
。
谢谢!
I have a UIViewController
class called AppController.h
, AppController.m
. I have thousands of lines of code in there, and that is the only reason why I didn't test this before I asked it. Is there any possible way to use drawRect
in a UIViewController
? that way I wouldn't have to make more delegates and methods and callbacks. I should have started off using drawRect
to handle my drawing code, but I didn't, and there's severe lag with core graphics on the iPad. So, please let me know if there's any way to use drawRect
in a UIViewController
.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
视图控制器自然有一个指向其视图的指针,因此您当然可以调用视图的
-setNeedsDisplay
方法,这将导致框架在适当的时候调用-drawRect:
时间。不过,很难准确理解您所要求的内容......您是否希望在视图控制器中进行实际绘图?如果您尝试这样做,那么您实际上是在对抗框架 - 将绘图代码移到您的视图类中。A view controller naturally has a pointer to its view, so of course you can call the view's
-setNeedsDisplay
method, which will cause the framework to call-drawRect:
at the appropriate time. It's a little hard to understand exactly what you're asking, though... are you hoping to do the actual drawing in your view controller? You'd really be working against the framework if you try that -- move the drawing code into your view class instead.您只需调用
setNeedsDisplay
,系统就会要求您在下一个适当的时间进行绘制。 (阅读:不要直接调用drawRect:
)如果您确实需要您请求的行为,那么只需在创建缓存位图或
CGImage
后调用setNeedsDisplay
code>/UIImage
是外部更新的(例如在控制器逻辑中),然后在请求绘制时绘制该图像。you just call
setNeedsDisplay
and you will be requested to draw at the next appropriate time. (read: don't calldrawRect:
directly)if you really need the behaviour you request, then just call
setNeedsDisplay
after creating a cached bitmap orCGImage
/UIImage
which is externally updated (e.g. in your controller logic) and then just draw that image when requested to draw.您应该(重构/重写并)创建 UIView (而不是视图控制器)的子类,以便在需要进行任何绘图时使用正确的绘图上下文调用该视图的 drawRect 委托。如果可能的话,在没有适当的绘图上下文的情况下尝试进行绘图可能会非常缓慢。
从技术上讲,您可以分配并创建自己的位图绘制上下文,将其放入具有drawRect方法的自定义对象中,然后调用该drawRect和上下文。但是直接绘制到自定义绘图上下文中可能会更快。
You should (refactor/rewrite and) create a subclass of a UIView (not a view controller) in order to have that view's drawRect delegate called with a proper drawing context when you need to do any drawing. Trying to draw without a proper drawing context can be painfully slow, if at all possible.
Technically, you could allocate and create your own bitmap drawing context, put in in a custom object with a drawRect method, and call that drawRect and context. But then it might be just faster to just draw directly into your custom drawing context.