Как создать подкласс UIView
и как использовать его с drawRect
для рисования графики?Нужно ли добавлять фреймворк, а затем включать в заголовочный файл QuartzCore или любой другой?
Комбинированные решения:
#import <UIKit/UIKit.h>
@interface drawingViewController : UIViewController {
}
@end
#import "drawingViewController.h"
#import "myview.h"
@implementation drawingViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect rect = CGRectMake(0,0,340,480);
myview *ui = [[myview alloc] initWithFrame:rect];
[self.view addSubview:ui];
}
#import <UIKit/UIKit.h>
@interface myview : UIView {
}
@end
#import "myview.h"
@implementation myview
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddArc(context, 50, 50, 50, 0, 30, 0);
CGContextFillPath(context);
//set the fill or stroke color
CGContextSetRGBFillColor(context, 1, 0.5, 0.5, 1.0);
CGContextSetRGBStrokeColor(context, 0.5, 1, 0.5, 1.0);
//fill or draw the path
CGContextDrawPath(context, kCGPathStroke);
CGContextDrawPath(context, kCGPathFill);
------------------------------
CGContextAddArc(context, 100, 100, 20, 0, 30, 0);
//set the fill or stroke color
CGContextSetRGBFillColor(context, 1, 0.5, 0.5, 1.0);
CGContextSetRGBStrokeColor(context, 0.5, 1, 0.5, 1.0);
//fill or draw the path
CGContextDrawPath(context, kCGPathStroke);
CGContextDrawPath(context, kCGPathFill);
}
другие примечания:
Рисование треугольника / стрелки на линии с CGContext
CGContextОптимизация
iPhone clear CGContext
Сохранение и восстановление CGContext
Как нарисовать линиюна iPhone?
Как нарисовать строку в других ориентациях устройства с помощью CGContext iOS