Objective c 如何将UIView子类化,然后如何将其与drawRect一起使用来绘制图形?

Objective c 如何将UIView子类化,然后如何将其与drawRect一起使用来绘制图形?,objective-c,cocoa-touch,Objective C,Cocoa Touch,如何子类化UIView,然后如何将其与drawRect一起使用以绘制图形? 我是否需要添加框架,然后将其包含在头文件QuartzCore或任何其他文件中 组合解决方案: 其他需要注意的事项: 在.h文件中 #import <QuartzCore/QuartzCore.h> @interface YourView : UIView { 然后调用setNeedsDisplay在.h文件中重新绘制rect #import <QuartzCore/QuartzCor

如何子类化
UIView
,然后如何将其与
drawRect
一起使用以绘制图形? 我是否需要添加框架,然后将其包含在头文件QuartzCore或任何其他文件中


组合解决方案:




其他需要注意的事项:

在.h文件中

#import <QuartzCore/QuartzCore.h>
@interface YourView : UIView {
然后调用
setNeedsDisplay
在.h文件中重新绘制rect

#import <QuartzCore/QuartzCore.h>
@interface YourView : UIView {

然后调用
setNeedsDisplay
重新绘制rect

为什么要将UIView子类化?为什么要将UIView子类化?
#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);

}
#import <QuartzCore/QuartzCore.h>
@interface YourView : UIView {
- (void)drawRect:(CGRect)rect{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //your drawings
}