IOS 开发学习 十四 与CGRect相关的几个结构体和画图有关的几个函数

aView.center = CGPointMake(150, 150); // set center

aView.frame = CGRectMake( 100, 200, aView.frame.size.width, aView.frame.size.height ); // set new position exactly

aView.frame = CGRectOffset( aView.frame, 10, 10 ); // offset by an amount

点:

struct CGPoint {  
  CGFloat x;  
  CGFloat y;  
};  
typedef struct CGPoint CGPoint;  

Size

    /* Sizes. */  
      
    struct CGSize {  
      CGFloat width;  
      CGFloat height;  
    };  
    typedef struct CGSize CGSize;  

CGRect

    /* Rectangles. */  
      
    struct CGRect {  
      CGPoint origin;//偏移是相对父窗口的  
      CGSize size;  
    };  
    typedef struct CGRect CGRect;  

CGRectMake

    CG_INLINE CGRect  
    CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)  
    {  
      CGRect rect;  
      rect.origin.x = x; rect.origin.y = y;  
      rect.size.width = width; rect.size.height = height;  
      return rect;  
    }  

UIScreen

CGrect screenBounds = [ [UIScreen mainScreen]bounds];//返回的是带有状态栏的Rect  
CGRect viewBounds = [ [UIScreen mainScreen]applicationFrame];//不包含状态栏的Rect  
//screenBounds 与 viewBounds 均是相对于设备屏幕来说的  
//所以 screenBounds.origin.x== 0.0 ;   screenBounds.oringin.y = 0.0;   screenBounds.size.width == 320;  screenBounds.size.height == 480(或者其他分辨率有所差异)  
//所以 screenBounds.origin.x== 0.0 ;   screenBounds.oringin.y = 20.0;(因为状态栏的高度是20像素)   screenBounds.size.width == 320;  screenBounds.size.height == 480 

UIView

UIView* myView =[[ UIView alloc]initWithFrame:CGRectMake(0.0,0.0,200.0,400.0)];//这里创建了一块画布,定义了相对于父窗口的位置, 以及大小。  

UIWindow

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
self.window.backgroundColor = [UIColor grayColor];//给window设置一个背景色 

获取CGContextRef

CGContextRef context = UIGraphicsGetCurrentContext();

无框矩形

    //设置矩形填充颜色:红色  
    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);  
    //填充矩形  
    CGContextFillRect(context, rect);  
    //执行绘画  
    CGContextStrokePath(context);  


有框矩形

    //设置矩形填充颜色:红色  
    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);  
    //填充矩形  
    CGContextFillRect(context, rect);  
    //设置画笔颜色:黑色  
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);  
    //设置画笔线条粗细  
    CGContextSetLineWidth(context, 1.0);  
    //画矩形边框  
    CGContextAddRect(context,rect);  
    //执行绘画  
    CGContextStrokePath(context);  


文字线条等

    //设置画笔线条粗细  
    CGContextSetLineWidth(context, 1.0);  
    //设置矩形填充颜色:红色  
    CGContextSetRGBFillColor (context, 1.0, 0.0, 0.0, 1.0);  
    //设置字体  
    UIFont *font = [UIFont boldSystemFontOfSize:31.0];  
    //在指定的矩形区域内画文字  
    [text drawInRect:rect withFont:font];  

画线

    //设置画笔线条粗细  
    CGContextSetLineWidth(context, 5.0);  
    //设置线条样式  
    CGContextSetLineCap(context, kCGLineCapButt);  
    //设置画笔颜色:黑色  
    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);  
    //画点连线  
    CGContextAddLines(context, points, count);  
    //执行绘画  
    CGContextStrokePath(context);  

椭圆

CGRect aRect= CGRectMake(80, 80, 160, 100);
   CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0);
   CGContextSetLineWidth(context, 3.0);
//   CGContextSetFillColorWithColor(context, aColor.CGColor);
//   CGContextAddRect(context, rect); //矩形
   CGContextAddEllipseInRect(context, aRect); //椭圆
   CGContextDrawPath(context, kCGPathStroke);

弧线

CGContextBeginPath(context);  
CGContextSetRGBStrokeColor(context, 0, 1, 0, 1); 
CGContextAddArc(context, 100, 100, 50, 180* PI/ 180, 270* PI/ 180,0);  CGContextStrokePath(context); 
屏幕快照

#import "QuartzCore/QuartzCore.h"

UIGraphicsBeginImageContext(yourView.frame.size); 
[[yourView layer] renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程圈子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值