@implementation MyView- (void)drawRect:(CGRect)rect { UIBezierPath * path=[[UIBezierPath alloc]init]; [[UIColor brownColor]setStroke];//设置绘图上下文的颜色。上下文在一个进程内部是单例的,所以可以设置。在setStroke内部会取得当前绘图上下文。 [path moveToPoint:CGPointMake(0, 0)]; [path addLineToPoint:CGPointMake(50, 50)]; [path moveToPoint:CGPointMake(50, 0)]; [path addLineToPoint:CGPointMake(100, 100)]; [path stroke];//表示绘制图形 //上面其实是绘制了两条线}@end
#import "ViewController.h"#import "MyView.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CGRect frame=CGRectMake(20, 20, 100, 100); UIView* view= [[MyView alloc]initWithFrame:frame]; [self.view addSubview:view];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end