// // AView.m // AutoLayout // // Created by ZhuYi on 16/5/24. // Copyright © 2016年 ZY. All rights reserved. // #import "AView.h" @implementation AView // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code UIImage *image = [UIImage imageNamed:@"dropdown_anim__0005"]; [image drawInRect:CGRectMake(, , rect.size.width, rect.size.height)]; // [image drawAsPatternInRect:CGRectMake(0, 0, rect.size.width, rect.size.height)]; } /** * 画三角形 */ void drawTriangel(){ //获得当前图形的上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); //设置七点 CGContextMoveToPoint(ctx, , ); CGContextAddLineToPoint(ctx, , ); CGContextAddLineToPoint(ctx, , ); // CGContextAddLineToPoint(ctx, 0, 0); //关闭路径 CGContextClosePath(ctx); // [[UIColor redColor] setFill]; // [[UIColor redColor] set]; CGContextSetRGBFillColor(ctx, , , , ); CGContextFillPath(ctx); } /** * 画矩形 */ void drawRect(){ CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextAddRect(ctx, CGRectMake(, , , )); // [[UIColor redColor] setStroke]; CGContextSetRGBStrokeColor(ctx, , , , ); CGContextStrokePath(ctx); } /** * 设置状态 */ void set(){ CGContextRef ctx = UIGraphicsGetCurrentContext(); //设置开头和结尾的样式 CGContextSetLineCap(ctx, kCGLineCapRound); //设置转折点的样式 CGContextSetLineJoin(ctx, kCGLineJoinRound); CGContextMoveToPoint(ctx, , ); CGContextAddLineToPoint(ctx, , ); CGContextSetLineWidth(ctx, ); CGContextSetRGBStrokeColor(ctx, , , , ); CGContextStrokePath(ctx); CGContextMoveToPoint(ctx, , ); CGContextAddLineToPoint(ctx, , ); CGContextAddLineToPoint(ctx, , ); CGContextSetRGBStrokeColor(ctx, , , , ); CGContextSetLineWidth(ctx, ); CGContextStrokePath(ctx); } /** * 画椭圆 */ void ellipse(){ CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSetRGBStrokeColor(ctx, , , , ); CGContextSetLineWidth(ctx, ); CGContextAddEllipseInRect(ctx, CGRectMake(, , , )); CGContextStrokePath(ctx); } /** * 画圆弧 */ void arr(){ CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextAddArc(ctx, , , , , -M_PI_4, ); CGContextSetLineWidth(ctx, ); CGContextSetRGBStrokeColor(ctx, , , , ); CGContextStrokePath(ctx); } /** * 画圆弧 */ void cusarr(){ CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextMoveToPoint(ctx, , ); CGContextAddLineToPoint(ctx, , ); CGContextAddArc(ctx, , , , M_PI_2, M_PI, ); CGContextClosePath(ctx); CGContextFillPath(ctx); } /** * 画文字 */ void drawText() { // 1.获得上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.画矩形 CGRect cubeRect = CGRectMake(, , , ); CGContextAddRect(ctx, cubeRect); // 3.显示所绘制的东西 CGContextFillPath(ctx); // 4.画文字 NSString *str = @"哈哈哈哈Good morning hello hi hi hi hi"; // [str drawAtPoint:CGPointZero withAttributes:nil]; NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; // NSForegroundColorAttributeName : 文字颜色 // NSFontAttributeName : 字体 attrs[NSForegroundColorAttributeName] = [UIColor redColor]; attrs[NSFontAttributeName] = [UIFont systemFontOfSize:]; [str drawInRect:cubeRect withAttributes:attrs]; } void drawImage() { // 1.取得图片 UIImage *image = [UIImage imageNamed:@"me"]; // 2.画 // [image drawAtPoint:CGPointMake(50, 50)]; // [image drawInRect:CGRectMake(0, 0, 150, 150)]; [image drawAsPatternInRect:CGRectMake(, , , )]; // 3.画文字 // NSString *str = @"为xxx所画"; // [str drawInRect:CGRectMake(0, 180, 100, 30) withAttributes:nil]; } /** * 矩阵操作和上下文栈 */ void juzhencaozuo(){ CGContextRef ctx = UIGraphicsGetCurrentContext(); //保存上下文栈 CGContextSaveGState(ctx); //矩阵操作 CGContextRotateCTM(ctx, M_PI_4 * 0.3); CGContextScaleCTM(ctx, 0.5, 0.5); CGContextTranslateCTM(ctx, , ); CGContextAddRect(ctx, CGRectMake(, , , )); CGContextStrokePath(ctx); //回复上下文栈 CGContextRestoreGState(ctx); CGContextAddEllipseInRect(ctx, CGRectMake(, , , )); CGContextMoveToPoint(ctx, , ); CGContextAddLineToPoint(ctx, , ); // 矩阵操作 // CGContextScaleCTM(ctx, 0.5, 0.5); CGContextStrokePath(ctx); } /** * 裁剪 */ void caijian(){ CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSaveGState(ctx); // 0.画圆 CGContextAddEllipseInRect(ctx, CGRectMake(, , , )); // 裁剪 CGContextClip(ctx); CGContextFillPath(ctx); // 1.显示图片 UIImage *image = [UIImage imageNamed:@"me"]; [image drawAtPoint:CGPointMake(, )]; } /** * 刷帧 */ - (void)setRadios:(float)radios{ _radios = radios; [self setNeedsDisplay]; } - (void)shuazhen{ ) { self.radios = ; } CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextAddArc(ctx, , , self.radios, , M_PI * , ); CGContextStrokePath(ctx); } @end
http://www.jianshu.com/p/734b34e82135