//spa
// ViewController.m.net
// Quartz2DTest3d
//orm
// Created by dc008 on 15/12/7.ip
// Copyright © 2015年 CXY. All rights reserved.get
//it
#import "ViewController.h"io
#import "MyView.h"class
@interface ViewController ()import
@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);
}