iOS之CoreGraphics使用(1)

http://my.oschina.net/CarlHuang/blog/138641


CoreGraphics.framework 是iOS 内置的用于画图的框架,可以画自定义的几何图形,它支持图形上下文、加载图像、绘制图像,等等。

      下面是我今天练习的代码:

     (1)代码1:绘制字符串

?
1
2
3
4
5
6
7
8
9
- ( void )drawRect:(CGRect)rect
{
     // Drawing code
     UIColor * magentaColor = [UIColor colorWithRed:0.5f green:0.0f blue:0.5f alpha:1.0f];
     [magentaColor set];
     UIFont * helveticaBold = [UIFont fontWithName:@ "HelveticaNeue-Bold" size:30.0f];
     NSString * myString = @ "I Learn Really Fast" ;
     [myString drawAtPoint:CGPointMake(25, 190) withFont:helveticaBold];
}
    (2)代码2:绘制图像

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-( void )drawRect:(CGRect)rect
{
     UIImage * image = [UIImage imageNamed:@ "xcode.png" ];
     if (image != nil)
     {
         NSLog(@ "Successfully loaded the image" );
     }
     else
     {
         NSLog(@ "Failed to load the image" );
     }
     
     [image drawAtPoint:CGPointMake(0.0f, 20.0f)];
     [image drawInRect:CGRectMake(50.0f, 10.0f, 40.0f,35.0f)];
     
}
    (3)代码3:绘制线段

?
1
2
3
4
5
6
7
8
9
-( void )drawRect:(CGRect)rect
{
     [[UIColor brownColor] set];
     CGContextRef currentContext = UIGraphicsGetCurrentContext();
     CGContextSetLineWidth(currentContext, 5.0f);
     CGContextMoveToPoint(currentContext, 50.0f, 10.0f);
     CGContextAddLineToPoint(currentContext, 100.0f, 200.0f);
     CGContextStrokePath(currentContext);
}
   (4)绘制两条相连的线段

?
1
2
3
4
5
6
7
8
9
10
-( void )drawRect:(CGRect)rect
{
     [[UIColor brownColor] set];
     CGContextRef currentContext = UIGraphicsGetCurrentContext();
     CGContextSetLineWidth(currentContext, 5.0f);
     CGContextMoveToPoint(currentContext, 20.0f, 20.0f);
     CGContextAddLineToPoint(currentContext, 100.0f, 100.0f);
     CGContextAddLineToPoint(currentContext, 300.0f, 100.0f);
     CGContextStrokePath(currentContext);
}
   (5)绘制屋顶demo

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-( void )drawRect:(CGRect)rect
{
     [self drawRooftopAtTopPointof:CGPointMake(160.0f, 40.0f) textToDisplay:@ "Miter" lineJoin:kCGLineJoinMiter];
     [self drawRooftopAtTopPointof:CGPointMake(160.0f, 180.0f) textToDisplay:@ "Bevel" lineJoin:kCGLineJoinBevel];
     [self drawRooftopAtTopPointof:CGPointMake(160.0f, 320.0f) textToDisplay:@ "Round" lineJoin:kCGLineJoinRound];
}
 
 
-( void )drawRooftopAtTopPointof:(CGPoint)paramTopPoint textToDisplay:(NSString * )paramText lineJoin:(CGLineJoin)paramLineJoin
{
     [[UIColor brownColor] set];
     CGContextRef currentContext = UIGraphicsGetCurrentContext();
     CGContextSetLineJoin(currentContext,paramLineJoin);
     CGContextSetLineWidth(currentContext, 20.0f);
     CGContextMoveToPoint(currentContext, paramTopPoint.x - 140, paramTopPoint.y + 100);
     CGContextAddLineToPoint(currentContext, paramTopPoint.x, paramTopPoint.y);
     CGContextAddLineToPoint(currentContext, paramTopPoint.x + 140, paramTopPoint.y + 100);
     CGContextStrokePath(currentContext);
     [[UIColor blackColor] set];
     [paramText drawAtPoint:CGPointMake(paramTopPoint.x - 40.0f, paramTopPoint.y + 60.0f) withFont:[UIFont boldSystemFontOfSize:30.0f]];
}
   (6)绘制矩形

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
-( void )drawRect:(CGRect)rect
{
     CGMutablePathRef path = CGPathCreateMutable();
     CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);
     CGPathAddRect(path, NULL, rectangle);
     CGContextRef currentContext = UIGraphicsGetCurrentContext();
     CGContextAddPath(currentContext, path);
     [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
     [[UIColor brownColor] setStroke];
     CGContextSetLineWidth(currentContext, 5.0f);
     CGContextDrawPath(currentContext, kCGPathFillStroke);
     CGPathRelease(path);
     
}
   (7)同时绘制多个矩形

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-( void )drawRect:(CGRect)rect
{
     CGMutablePathRef path = CGPathCreateMutable();
     CGRect rectangle1 = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);
     CGRect rectangle2 = CGRectMake(40.0f, 100.0f, 90.0f, 300.0f);
     CGRect rectangles[2] = {rectangle1,rectangle2};
     CGPathAddRects(path, NULL, ( const CGRect *)&rectangles, 2);
     CGContextRef currentContext = UIGraphicsGetCurrentContext();
     
     CGContextAddPath(currentContext, path);
     [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
     [[UIColor blackColor] setStroke];
     
     CGContextDrawPath(currentContext, kCGPathFillStroke);
     CGPathRelease(path);
     
     
}
  (8)给几何图形添加阴影

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-( void )drawRectAtTopOfScreen
{
     CGContextRef currentContext = UIGraphicsGetCurrentContext();
     CGContextSaveGState(currentContext);
     CGContextSetShadowWithColor(currentContext, CGSizeMake(10.0f, 10.0f), 20.0f, [[UIColor grayColor] CGColor]);
     CGMutablePathRef path = CGPathCreateMutable();
     CGRect firstRect = CGRectMake(55.0f, 60.0f, 150.0f, 150.0f);
     CGPathAddRect(path, NULL, firstRect);
     CGContextAddPath(currentContext, path);
     [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
     CGContextDrawPath(currentContext, kCGPathFill);
     CGPathRelease(path);
     CGContextRestoreGState(currentContext);
}
 
-( void )drawRectAtBottomOfScreen
{
     CGContextRef currentContext = UIGraphicsGetCurrentContext();
     CGMutablePathRef path = CGPathCreateMutable();
     CGRect secondRect = CGRectMake(150.0f, 250.0f, 100.0f, 100.0f);
     CGPathAddRect(path, NULL
                   , secondRect);
     CGContextAddPath(currentContext, path);
     [[UIColor purpleColor] setFill];
     CGContextDrawPath(currentContext, kCGPathFill);
     CGPathRelease(path);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值