Any possible way to call drawRect from a UIViewcontroller class?

旧街凉风 提交于 2019-12-02 03:39:56

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.

you just call setNeedsDisplay and you will be requested to draw at the next appropriate time. (read: don't call drawRect: directly)

if you really need the behaviour you request, then just call setNeedsDisplay after creating a cached bitmap or CGImage/UIImage which is externally updated (e.g. in your controller logic) and then just draw that image when requested to draw.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!