画直线

头文件:

 

#import <Foundation/Foundation.h>

@interface DrawLine : UIView {

}

@end

 

实现文件:

 

#import "DrawLine.h"

@implementation DrawLine

- (id)initWithFrame:(CGRect)frame {
	if (self = [super initWithFrame:frame]) {
		self.backgroundColor = [UIColor orangeColor];
	}
	return self;
}

- (void)drawRect:(CGRect)rect {
	[super drawRect:rect];
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSetRGBStrokeColor(context, 0.05, 0.25, 0.5, 1.0);
	CGContextSetLineWidth(context, 1.0);
	int i = 0;
	while(i < 101){
		CGContextMoveToPoint(context, 0, i);
		CGContextAddLineToPoint(context, 320, i);
		CGContextStrokePath(context);
		i += 20;
	}
}

@end

 

示例:

 

#import "LineViewController.h"

@implementation LineViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	DrawLine *lineView = [[DrawLine alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
	[self.view addSubview:lineView];
	[lineView release];
}

- (void)dealloc {
	[super dealloc];
}

@end

 

示例图:


你可能感兴趣的:(ios,iPhone,drawRect)