With UIKit, everything that the user sees and interacts with is essentially a view. For instance, a button is a view, a table view is a view, etc. Your application should already have a main view controller, or a main window, which can contain zero or more views. You can draw on any view that you create, as long as you are in the position to implement the drawRect:
method. Here's an example of implementing a UIView
subclass on which can you can draw using CoreGraphics:
@interface MyView : UIView
@end
@implementation MyView
- (void)drawRect:(CGRect)dirtyRect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 0, 0, 0, 1);
// here, you can draw shapes, for example, a circle:
CGContextFillEllipseInRect(context, CGContextMake(10, 10, 50, 50));
}
@end
Then, in order to add an instance of MyView
to your application, either drag in a custom view in interface builder and change its class to MyView
, or do this in viewDidLoad
of your view controller:
- (void)viewDidLoad {
MyView * aView = [[MyView alloc] initWithFrame:self.bounds];
[self.view addSubview:aView];
#if __has_feautre(objc_arc) != 1
[aView release];
#endif
}
You can learn more about what you can do in the drawRect
method from the Quartz 2D Programming Guide.