天天看點

Cocoa畫圖

points(NSPoint)

同直角坐标系一樣原點在左下角

結構

typedef struct _NSPoint{

float x;

float y;

}NSPoint;

定義:NSPoint thePoint;

初始化:thePoint=NSMakePoint(0,0);

Rects(NSRect) and sizes(NSSize)

typedef struct _NSRect{

NSPoint origin;

NSSize size;

}NSRect;

typedef struct _NSSize{

float width;

float height;

}NSSize;

定義:NSRect theRect;

初始化:theRect=NSMakeRect(0,0,100,100);//(x,y,width,height

使用:theRect.origin.x , theRect.origin.y

theRect.size.width , theRect.size.height

Colors(NSColor)

兩種方法初始化

簡單方式,用系統已提供的顔色名稱

NSColor *aColor;

aColor=[NSColor blackColor];

blackColor Black

blueColor Bright blue

brownColor Brown

clearColor Clear/transparent

cyanColor Light blue

DarkGrayColor Dark gray(for the Canadians,dark grey)

grayColor Medium gray

greenColor Bright green

lightGrayColor Light gray

magentaColor Pinkish purple color

orangeColor Orange

purpleColor Purple

redColor Bright red

whiteColor White

yellowColor Bright and sunny yellow

自己建立顔色

Device dependent(or device)

DeviceRGB:(Red , green , blue and alpha components)

//參數值為0-1,顔色表示[(num +1)/256],alpha表不透明度

NSColor=theColor;//black

theColor=[NSColor colorWithDeviceRed:(float)0.0,

green:(float)0.0 blue:(float)0.0 alpha:(float)1.0];

[theColor set];//設定畫筆

DeviceCMYK:(Cyan , magenta , yellow , black and alpha components)

DeviceWhite:(White and alpha components)

Device  independent(or calibrated)

Named

用線和圖形繪畫

利用點成線,線成面的原理

畫一個矩形

NSRect theRect;

theRect=NSMakeRect(0,0,100,100);

NSBezierPath *thePath;

thePath=[NSBezierPath bezierPathWithRect:theRect];

//bezierPathWithOvalInRect 畫橢圓

用顔色填充矩形

NSColor *theColor=[NSColor blackColor];

[theColor set];

[thePath fill];

畫一條線

畫一條線就相當于在一條線周圍描個框,并填充它

在上面已填充的矩形周圍描框,需要的操作同上相反

theColor=[NSColor blackColor];

[thePath setLineWidth:5];

[thePath stroke];

畫任意圖形

用多個點連成線然後填充構成圖形,有起始點,其它點以起始點為相對坐标,移動圖形時僅需要改變起始點坐标

+X:X右邊; -X:X左邊; +Y:Y上邊; -Y:Y下邊

NSBezierPath *stopSign=[NSBezierPath bezierPath];

NSPoint pt1,pt2,pt3,pt4,pt5;//五邊形

pt1=NSMakePoint(300,300);

pt2=NSMakePoint(400,300);

pt3=NSMakePoint(450,200);

pt4=NSMakePoint(350,100);

pt5=NSMakePoint(250,200);

[stopSign moveToPoint:pt1];

[stopSign relativeLineToPoint:pt2];

[stopSign relativeLineToPoint:pt3];

[stopSign relativeLineToPoint:pt4];

[stopSign relativeLineToPoint:pt5];

[stopSign closePath];

[[NSColor redColor] set];

[stopSign fill];

[[NSColor whiteColor]set];

[stopSign setLineWide:5];

[stopSign stroke];

畫文字(Text)

NSString *theString=@"SHOW";

NSPoint theTextPos=NSMakePoint(100,120);

[theString drqwAtPoint:theTextPos withAttributes:nil];

可通過設定withAttributes方法(需要NSDictionary類型變量)修改文字顯示樣式

NSMutableDictionary *theAttributes=[[NSMutableDictionary alloc] init];

[theAttributes setObject:[NSFont fontWithName:@"Helvetica" size:62] forKey:NSFontAttributeName];

[theAttributes setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];

[theString drawAtPoint:theTextPos withAttributes:theAttributes];

[theAttrubutes release];

顯示圖檔

把一張圖檔拉到Xcode工程中

NSPoint theImagePos=NSMakePoint(0,0)

NSImage * theImage=[NSImage imageNamed:@"mypic.jpg"];

[theImage dissolveToPoint:theImagePos fraction:(1.0)];

//fraction的值表示圖檔顯示的不透明度:1表不透明