AppDelegate.m
Myview *view = [[Myview alloc] initWithFrame:CGRectMake(10, 50, 300, 300)];
[self.window addSubview:view];
MyView.m
#import "Myview.h"
@implementation Myview
CGFloat arc(CGFloat angle)
{
return angle*M_PI/180;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor orangeColor];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
//对比rect和frame、bounds。rect就是view的大小
NSLog(@"frame:%@",NSStringFromCGRect(self.frame));
NSLog(@"bounds:%@",NSStringFromCGRect(self.bounds));
NSLog(@"rect:%@",NSStringFromCGRect(rect));
//画线
// [self drawLine];
//画矩形
// [self drawRectangele];
//画曲线
// [self drawArc];
//画文字
// [self drawText];
//画图片
[self drawImage];
}
//画线
- (void)drawLine
{
//图像上下文,获取画布,它是一个结构体指针
CGContextRef context = UIGraphicsGetCurrentContext();
//获取画笔,设置起点
CGContextMoveToPoint(context, 10, 10);
//移动画笔
CGContextAddLineToPoint(context, 10, 200);
CGContextAddLineToPoint(context, 200, 200);
//设置宽度
CGContextSetLineWidth(context, 5);
//设置圆角
CGContextSetLineCap(context, kCGLineCapRound);
//设置虚线
//CGContextRef: 画布
//phase:虚线开始填充的大小
//const CGFloat *lengths:虚线排列的大小规则
//size_t count:排列的个数
CGFloat arr[] = {10,10};
CGContextSetLineDash(context, 0, arr, 2);
//设置画笔颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//保存路劲,第二个参数决定绘画的样式
CGContextDrawPath(context, kCGPathStroke);
}
//画矩形
- (void)drawRectangele
{
CGContextRef context = UIGraphicsGetCurrentContext();
//绘制矩形
CGContextAddRect(context, CGRectMake(20, 20, 250, 250));
//设置矩形边框颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//设置填充颜色
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
//设置宽度
CGContextSetLineWidth(context, 5);
// //设置虚线
// CGFloat arr[] = {10,4};
// CGContextSetLineDash(context, 0, arr, 2);
CGContextDrawPath(context, kCGPathFillStroke);
}
//画曲线
- (void)drawArc
{
CGContextRef context = UIGraphicsGetCurrentContext();
//设置矩形边框颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//设置填充颜色
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
//设置宽度
CGContextSetLineWidth(context, 2);
//贝塞尔曲线1
//设置起点
// CGContextMoveToPoint(context, 10, 50);
// //参数是贝塞尔曲线的参数
// CGContextAddCurveToPoint(context, 50, 30, 200, 300, 300, 80);
//贝塞尔曲线2
// CGContextAddQuadCurveToPoint(context, 55, 0, 100, 50);
//画扇形/圆形
CGContextAddArc(context, self.frame.size.width/2, self.frame.size.height/2, 100, arc(-90), arc(0), 0);
CGContextDrawPath(context, kCGPathFillStroke);
}
//画文字
- (void)drawText
{
NSString *text = @"你好";
[text drawInRect:CGRectMake(10, 10, 200, 30) withAttributes:nil];
}
//画图片
- (void)drawImage
{
// UIImage *img = [UIImage imageNamed:@"image"];
// [img drawInRect:self.bounds];
Color Declarations
Color Declarations
UIColor* color = [UIColor colorWithRed: 0.226 green: 0.269 blue: 0.78 alpha: 1];
Star Drawing
UIBezierPath* starPath = UIBezierPath.bezierPath;
[starPath moveToPoint: CGPointMake(63, 97)];
[starPath addLineToPoint: CGPointMake(71.82, 84.14)];
[starPath addLineToPoint: CGPointMake(86.78, 79.73)];
[starPath addLineToPoint: CGPointMake(77.27, 67.36)];
[starPath addLineToPoint: CGPointMake(77.69, 51.77)];
[starPath addLineToPoint: CGPointMake(63, 57)];
[starPath addLineToPoint: CGPointMake(48.31, 51.77)];
[starPath addLineToPoint: CGPointMake(48.73, 67.36)];
[starPath addLineToPoint: CGPointMake(39.22, 79.73)];
[starPath addLineToPoint: CGPointMake(54.18, 84.14)];
[starPath closePath];
[color setFill];
[starPath fill];
}
@end