美文网首页iOS开发iOS Developer程序员
Sprite Kit中anchorPoint和position的

Sprite Kit中anchorPoint和position的

作者: e40c669177be | 来源:发表于2016-11-01 14:02 被阅读157次

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];

相关文章

网友评论

    本文标题:Sprite Kit中anchorPoint和position的

    本文链接:https://www.haomeiwen.com/subject/mlfputtx.html