//
// ViewController.m
// Quartz2DTest
//
// Created by dc008 on 15/12/7.
// Copyright © 2015年 CXY. All rights reserved.
//
#import "ViewController.h"
#import "MyView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyView *myView = [[MyView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
myView.backgroundColor = [UIColor grayColor];
[self.view addSubview:myView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//
// MyView.m
// Quartz2DTest
//
// Created by dc008 on 15/12/7.
// Copyright © 2015年 CXY. All rights reserved.
//
#import "MyView.h"
@implementation MyView
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self drawLine];
[self drawEllipse:ctx];
[self drawOneCurve:ctx];
}
//设置线
- (void)drawLine{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRef contextOne = UIGraphicsGetCurrentContext();
CGContextRef contextTwo = UIGraphicsGetCurrentContext();
CGContextRef contextFour = UIGraphicsGetCurrentContext();
CGContextRef contextFive = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 140, 300);
CGContextAddLineToPoint(context,260, 300);
CGContextMoveToPoint(contextOne,167, 340);
CGContextAddLineToPoint(contextOne, 230, 340);
CGContextMoveToPoint(contextTwo, 120, 340);
CGContextAddLineToPoint(contextTwo, 140, 340);
CGContextMoveToPoint(contextFour, 260, 340);
CGContextAddLineToPoint(contextFour, 280, 340);
CGContextMoveToPoint(contextFive, 160, 320);
CGContextAddLineToPoint(contextFive, 230, 320);
[[UIColor blackColor]set];
CGContextSetLineWidth(context, 3);
CGContextSetLineWidth(contextOne, 3);
CGContextSetLineWidth(contextTwo, 3);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextDrawPath(contextOne, kCGPathFillStroke);
CGContextDrawPath(contextTwo, kCGPathFillStroke);
}
//设置轮胎(圆)
- (void)drawEllipse : (CGContextRef)context{
CGRect rectOne = CGRectMake(140, 330, 30, 30);
CGRect rectTwo = CGRectMake(230, 330, 30, 30);
CGRect rectThree = CGRectMake(240, 340, 10, 10);
CGRect rectFour = CGRectMake(150, 340, 10, 10);
CGContextAddEllipseInRect(context, rectOne);
CGContextAddEllipseInRect(context, rectTwo);
CGContextAddEllipseInRect(context, rectThree);
CGContextAddEllipseInRect(context, rectFour);
[[UIColor blackColor]setStroke];
CGContextDrawPath(context, kCGPathStroke);
}
//#pragma mark 绘制弧
//- (void)drawArc : (CGContextRef) context {
// CGContextAddArc(context, 375/2.0, 667/2.0, 100, 0.0, M_PI_2, 1);
// //设置属性
// [[UIColor orangeColor]setStroke];
// //绘制
// CGContextDrawPath(context, kCGPathStroke);
//}
#pragma mark 绘制贝塞尔曲线
- (void) drawOneCurve : (CGContextRef) context {
CGContextMoveToPoint(context, 280, 340);//起点位置
//cp1x:第一个控制点x坐标
//cp1y:第一个控制点y坐标
//cp2x:第二个控制点x坐标
//cp2y:第二个控制点y坐标
//x:结束点x坐标
//y:结束点y坐标
CGContextAddQuadCurveToPoint(context, 280, 300, 260, 300);//cp1x和cp1y是终点位置
//设置曲线属性
[[UIColor blackColor]setStroke];
//绘制
CGContextDrawPath(context, kCGPathStroke);
CGContextMoveToPoint(context, 120, 340);
CGContextAddQuadCurveToPoint(context, 100, 300, 140, 300);
[[UIColor blackColor]setStroke];
CGContextDrawPath(context, kCGPathStroke);
}
@end