anchorPoint:通過position來控制它的frame的位置,可以認為anchorPoint是用來移動圖層的把柄
一般來說anchorPoint:位于圖層的中心,但是圖層的anchorPoint可以被移動.
好了,將錨點之前,先來了解下坐標系
常見的三維坐標系分為兩種:左手坐標系和右手坐標系.當確定了x軸,y軸方向之后可以通過左手或右手來確定z軸的方向.
坐標系
拇指指向x軸正方向,食指指向y軸正方向,中指指向z軸正方向.左手坐標系使用左手,右手坐標系使用右手.在Mac ,iOS中有一點是不變的,z軸的正方向總是指向觀察者,也就是垂直屏幕平面向上的
** iOS系統在使用左手坐標,但是Sprite Kit使用右手坐標,Sprite Kit使用右手坐標,Sprite Kit使用右手坐標,重要的事情說三遍**
iOS系統坐標
Sprite Kit坐標系
錨點的使用:錨點使用的是相對坐標,范圍是從0.0 ~1.0
默認值為0.5
錨點和position關于自身的使用
//anchorPoint = (0.5,0.5);
SKSpriteNode *spaceship = [[SKSpriteNode alloc] initWithColor:[UIColor orangeColor] size:CGSizeMake(100, 100)];
spaceship.position = CGPointMake(300, 300);
spaceship.anchorPoint = CGPointMake(0.5, 0.5);
[self addChild:spaceship];
錨點的使用
這種圖看的不是很清楚,使用坐標系創建
anchorPoint = (0.5,0.5),紅色的點代表(300,300)
//anchorPoint = (1,0.5)
SKSpriteNode *spaceship = [[SKSpriteNode alloc] initWithColor:[UIColor orangeColor] size:CGSizeMake(100, 100)];
spaceship.position = CGPointMake(300, 300);
spaceship.anchorPoint = CGPointMake(1, 0.5);
anchorPoint = (1,0.5)
//anchorPoint = (0.5,1)
SKSpriteNode *spaceship = [[SKSpriteNode alloc] initWithColor:[UIColor orangeColor] size:CGSizeMake(100, 100)];
spaceship.position = CGPointMake(300, 300);
spaceship.anchorPoint = CGPointMake(0.5, 1);
[self addChild:spaceship];
anchorPoint = (0.5,1)
父視圖上的錨點對子視圖的影響
-(SKSpriteNode *)makeRedBox:(CGSize )size{
return [[SKSpriteNode alloc] initWithColor:[UIColor redColor] size:size];
}
-(SKSpriteNode *)makeGreenBox:(CGSize)size{
return [[SKSpriteNode alloc] initWithColor:[UIColor greenColor] size:size];
}
-(SKSpriteNode *)makeBlueBox:(CGSize)size{
return [[SKSpriteNode alloc] initWithColor:[UIColor blueColor] size:size];
}
pragma mark ---1
SKSpriteNode *box1 = [self makeRedBox:CGSizeMake(100, 100)];
box1.anchorPoint = CGPointMake(0, 0);
box1.position = CGPointMake(100, 100);
SKSpriteNode *box2 = [self makeBlueBox:CGSizeMake(50, 50)];
box2.anchorPoint = CGPointMake(0, 0);
box2.position = CGPointMake(0, 0);
[box1 addChild:box2];
SKSpriteNode *box3 = [self makeGreenBox:CGSizeMake(25, 25)];
box3.anchorPoint = CGPointMake(0, 0);
box3.position = CGPointMake(0, 0);
[box2 addChild:box3];
[self addChild:box1];
一
紅色的anchorPoint(0,0):說明紅色方塊的左下角在(100,100)點,紅色的坐標原點在左下角
藍色的anchorPoint(0,0),position(0,0):說明藍色的左下角在紅色的(0,0)點,坐標原點在藍色的左下角
綠色的anchorPoint(0,0),position(0,0):綠色的左下角在藍色的(0,0)點位置
pragma mark ---二
SKSpriteNode *box1 = [self makeRedBox:CGSizeMake(100, 100)];
box1.anchorPoint = CGPointMake(0.5, 0.5);
box1.position = CGPointMake(100, 100);
SKSpriteNode *box2 = [self makeBlueBox:CGSizeMake(50, 50)];
box2.anchorPoint = CGPointMake(0, 0);
box2.position = CGPointMake(0, 0);
[box1 addChild:box2];
SKSpriteNode *box3 = [self makeGreenBox:CGSizeMake(25, 25)];
box3.anchorPoint = CGPointMake(0, 0);
box3.position = CGPointMake(0, 0);
[box2 addChild:box3];
[self addChild:box1];
二
紅色的anchorPoint(0.5,0.5):說明紅色方塊的中心點在(100,100)點,紅色的坐標原點(0,0)在紅色的中心點
藍色的anchorPoint(0,0),position(0,0):說明藍色的左下角在紅色的(0,0)點,坐標原點在藍色的左下角
綠色的anchorPoint(0,0),position(0,0):綠色的左下角在藍色的(0,0)點位置
pragma mark----三
SKSpriteNode *box1 = [self makeRedBox:CGSizeMake(100, 100)];
box1.anchorPoint = CGPointMake(0.5, 0.5);
box1.position = CGPointMake(100, 100);
SKSpriteNode *box2 = [self makeBlueBox:CGSizeMake(50, 50)];
box2.anchorPoint = CGPointMake(0, 0);
box2.position = CGPointMake(25, 25);
[box1 addChild:box2];
SKSpriteNode *box3 = [self makeGreenBox:CGSizeMake(25, 25)];
box3.anchorPoint = CGPointMake(0, 0);
box3.position = CGPointMake(0, 0);
[box2 addChild:box3];
[self addChild:box1];
三
紅色的anchorPoint(0.5,0.5):說明紅色方塊的中心點在(100,100)點,紅色的坐標原點(0,0)在紅色的中心點
藍色的anchorPoint(0,0),position(25,25):說明藍色的左下角在紅色的(25,25)點,坐標原點在藍色的左下角
綠色的anchorPoint(0,0),position(0,0):綠色的左下角在藍色的(0,0)點位置
pragma mark ---四
SKSpriteNode *box1 = [self makeRedBox:CGSizeMake(100, 100)];
box1.anchorPoint = CGPointMake(0.5, 0.5);
box1.position = CGPointMake(100, 100);
SKSpriteNode *box2 = [self makeBlueBox:CGSizeMake(50, 50)];
box2.anchorPoint = CGPointMake(0.5, 0.5);
box2.position = CGPointMake(25, 25);
[box1 addChild:box2];
SKSpriteNode *box3 = [self makeGreenBox:CGSizeMake(25, 25)];
box3.anchorPoint = CGPointMake(0, 0);
box3.position = CGPointMake(0, 0);
[box2 addChild:box3];
[self addChild:box1];
四