现在的位置: 首页 > 综合 > 正文

cocoa touch layer下面的几个点连载之--CoreGraphics

2013年12月11日 ⁄ 综合 ⁄ 共 1888字 ⁄ 字号 评论关闭
画一个小原点的代码
----------------------
    UIGraphicsBeginImageContext(CGSizeMake(150, 150));//创建位图上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();//返回当前上下文
    CGContextBeginPath(ctx);//创建新路径
    CGContextAddArc(ctx, 10,10, 10, 0, 4*M_PI, 1);//使用狐制度创建狐
    CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);//设置颜色
    CGContextFillPath(ctx);//自动关闭路径,并通过填充对路径进行喷漆.
   
    UIImage *redball = UIGraphicsGetImageFromCurrentImageContext();//返回一个位图上下文,仅仅用于位图
    UIGraphicsEndImageContext();//关闭位图上下文   
    UIImageView *redballview = [[UIImageView alloc]initWithImage:redball];
    redballview.center = CGPointMake(200, 300);
    [self.view addSubview:redballview];
-----------------------------------------
CGContextAddArc是一个比较强大的函数CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, intclockwise)
CGContextRef: 图形上下文
x,y: 开始画的坐标
radius: 半径
startAngle, endAngle: 开始的弧度,结束的弧度
clockwise: 画的方向(顺时针,逆时针)
先要好好理解下上面那个函数,这个在上面的代码总来说是最难理解的,如果english nx可能就另当别论了.
无意中找到一个很叼的学习参考网站,放着记下

http://objective-j.org/learn/documentation/group__coregraphics.html#gac3f1c96dd9247e1ebc30d89b1b2acf13

在iPhone屏幕上画长方形,直线和文字
画扇形和画方形、画直线完全是两码事儿,
 
//画长方形
CGContextRef ctx = UIGraphicsGetCurrentContext();
//设置颜色,仅填充4条边
CGContextSetStrokeColorWithColor(ctx, [[UIColor colorWithRed:1 green:1 blue:1 alpha:0.5] CGColor]);
//设置线宽为1 
CGContextSetLineWidth(ctx, 1.0);
//设置长方形4个顶点
CGPoint poins[] = {CGPointMake(5, 5),CGPointMake(425, 5),CGPointMake(425, 125),CGPointMake(5, 125)};
CGContextAddLines(ctx,poins,4);
CGContextClosePath(ctx);
CGContextStrokePath(ctx);
 
//画直线,x1和y1是起始点,x2和y2是结束点
//默认坐标系左上角为0,0
CGContextMoveToPoint(ctx, x1, y1);
CGContextAddLineToPoint(ctx, x2, y2);
CGContextClosePath(ctx);
CGContextStrokePath(ctx);
 
//画文字,设置文字内容
NSString *text = @"text";
//设置字体大小
UIFont *font = [UIFont systemFontOfSize:8];
//在指定x,y点位置画文字,宽度为18
[text drawAtPoint:CGPointMake(x, y) forWidth:18 withFont:font
minFontSize:8 actualFontSize:NULL
lineBreakMode:UILineBreakModeTailTruncation
baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
[text release];

抱歉!评论已关闭.