四个图元:
1.点
2.线段
3.弧
4.贝塞尔曲线
实例:使用贝塞尔曲线
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建根视图控制器
ViewController* rootViewController = [[ViewController alloc] init];
//创建自定义视图
MyView* view = [[MyView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//用自定义视图替换系统默认视图
rootViewController.view = view;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
self.window.rootViewController = rootViewController;
return YES;
}
@end
#import "MyView.h"
@implementation MyView
- (void)drawRect:(CGRect)rect {
//填充白色背景
[[UIColor whiteColor] setFill];
UIRectFill(rect);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 333, 0);
CGContextAddCurveToPoint(context, 333, 0, 332, 26, 330, 26);
CGContextAddCurveToPoint(context, 330, 26, 299, 20, 299, 17);
CGContextAddLineToPoint(context, 296, 17);
CGContextAddCurveToPoint(context, 296, 17, 296, 19, 291, 19);
CGContextAddLineToPoint(context, 250, 19);
CGContextAddCurveToPoint(context, 250, 19, 241, 24, 238, 19);
CGContextAddCurveToPoint(context, 236, 20, 234, 24, 227, 24);
CGContextAddCurveToPoint(context, 220, 24, 217, 19, 216, 19);
CGContextAddCurveToPoint(context, 214, 20, 211, 22, 207, 20);
CGContextAddCurveToPoint(context, 207, 20, 187, 20, 182, 21);
CGContextAddLineToPoint(context, 100, 45);
CGContextAddLineToPoint(context, 97, 46);
CGContextAddCurveToPoint(context, 97, 46, 86, 71, 64, 72);
CGContextAddCurveToPoint(context, 42, 74, 26, 56, 23, 48);
CGContextAddLineToPoint(context, 9, 47);
CGContextAddCurveToPoint(context, 9, 47, 0, 31, 0, 0);
CGContextStrokePath(context);
}
@end
void CGContextAddCurveToPoint(
CGContextRef c,//创建CGContext对象(CGContextRef是CGContext的应用类型)
CGFloat cp1x,CGFloat cp1y,//第一控制点
CGFloat cp2x,CGFloat cp2y,//第二控制点
CGFloat x,CGFloat y,//端点