今天发现有些人被表格所难住了,我的想法是用 DrawRect进行表格的绘制,那么问题来了,对于 DrawRect我感觉只有大神对其会比较了解,于是我就试着写下关于 DrawRect的简单用法,至于更深的,还需要话时间去研究,今天只带来其最基本的用法,供大家有所了解!
#import <UIKit/UIKit.h>
@interface OneView : UIView
@end
#import "OneView.h"
@implementation OneView
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
[self drawLine];
}
#pragma mark - 画线
-(void)drawLine{
//第一步:获取上下文
//CGContextRef 用来保存图形信息.输出目标
CGContextRef context = UIGraphicsGetCurrentContext();
//第二步:画图形
//设置线的颜色
CGContextSetRGBStrokeColor(context, 255/255.0, 255/255.0, 255/255.0, 1);
//设置线的宽度
CGContextSetLineWidth(context, 13);
//设置连接点得样式
CGContextSetLineJoin(context, kCGLineJoinRound);
//设置线头尾的样式
CGContextSetLineCap(context, kCGLineCapRound);
//起点
CGContextMoveToPoint(context, 10, 20);
//画线
CGContextAddLineToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 100, 150);
//第三步:渲染到视图上
CGContextStrokePath(context);
}
- (void)drawARect{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 10, 10);
CGContextAddLineToPoint(context, 110, 10);
CGContextAddLineToPoint(context, 110, 110);
CGContextAddLineToPoint(context, 10, 110);
CGContextAddLineToPoint(context, 10, 10);
//第二种
CGContextAddRect(context, CGRectMake(0, 0, 100, 100));
CGContextStrokePath(context);
//填充
// CGContextFillPath(context);
//只画线
CGContextStrokePath(context);
}
//画圆
- (void)drawCircle{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(context, CGRectMake(0, 0, 100, 100));
CGContextStrokePath(context);
}
- (void)drawArc{
CGContextRef context = UIGraphicsGetCurrentContext();
/**
* <#Description#>
*
* @param c#> 上下文 description#>
* @param x#> 圆点x坐标 description#>
* @param y#> 圆点y坐标 description#>
* @param radius#> 半径 description#>
* @param startAngle#> 开始角度 description#>
* @param endAngle#> 结束角度 description#>
* @param clockwise#> 是否顺时针 description#>
*/
CGContextMoveToPoint(context, 100, 100);
CGContextAddArc(context, 100, 100, 60, 0, M_PI_4, 0);
//闭合
CGContextClosePath(context);
CGContextStrokePath(context);
}
- (void)drawText{
NSString *string = @"SDFDFWDF:FEEFF:DFWFEFW";
[string drawAtPoint:CGPointMake(100, 100) withAttributes:nil];
[string drawInRect:CGRectMake(0, 0, 100, 100) withAttributes:nil];
}
- (void)drawBeize{
//贝塞尔曲线
UIBezierPath *bezier = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:60 startAngle:0 endAngle:3.14*2 clockwise:YES];
UIColor *color = [UIColor orangeColor];
[color setFill];
[bezier setLineWidth:20];
[bezier fill];
}
#import "ViewController.h"
#import "OneView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
OneView *one = [[OneView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
one.center = self.view.center;
one.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:one];
}