Any possible way to call drawRect from a UIViewcon

2019-03-01 03:49发布

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!

3条回答
够拽才男人
2楼-- · 2019-03-01 04:22

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.

查看更多
地球回转人心会变
3楼-- · 2019-03-01 04:25

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.

查看更多
贼婆χ
4楼-- · 2019-03-01 04:31

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.

查看更多
登录 后发表回答