如何对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);
}
其他注意事项:
Drawing triangle/arrow on a line with CGContext
Saving and restoring CGContext
How do I draw a line on the iPhone?
How to draw string in other device orientations using CGContext iOS
答案 0 :(得分:0)
#import <QuartzCore/QuartzCore.h>
@interface YourView : UIView {
...
<。>文件中的
- (void)drawRect:(CGRect)rect{
CGContextRef ctx = UIGraphicsGetCurrentContext();
//your drawings
}
然后调用setNeedsDisplay
重绘rect