Subclassing UIView to draw in Quartz

2020-05-08 13:01发布

问题:

I'm trying to draw a figure in my iPhone application using Quartz 2D. I understand that I have to do this by subclassing UIView and overriding the drawRect function. I'm trying to follow this tutorial to do this but I can't get it to work.

It seems subclassing UIView does not create a .xib-file like the tutorial seem to imply. Subclassing UIViewController (I know this is wrong but I tried it anyway) does create a .xib-file but does not seem to have the drawRect-function.

Has anyone any experience in this, or knows what I'm doing wrong?

Thank you in advance, Tobias Tovedal

回答1:

  1. Create a UIView subclass and put your drawing code in its drawRect: method. You don't need a NIB file (why would you if you draw the view yourself?).

  2. In a view controller, create an instance of your custom view class, set the view's frame and add it to the view controller's view with: [self.view addSubview:myCustomView];.

  3. There is no step 3.