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();