问题
但是,按比例放大2D图像会从图像中心点开始按比例放大。我需要它从特定坐标向上扩展吗?
[backgroundImage setScale:rifleZoom];
我目前用于放大图像的技术。
我需要从中心屏幕放大,而不是中心图像
现在,我将步枪射击放在屏幕@中心上的方式是这样的:
CGPoint positionNow = CGPointMake(backgroundImage.position.x, backgroundImage.position.y);
CGPoint positionPrev = CGPointMake(0.5, 0.5);
float xdiff = positionNow.x - positionPrev.x;
float ydiff = positionNow.y - positionPrev.y;
CGPoint newPositionOne = CGPointMake(0.5- xdiff, 0.5 - ydiff);
newPositionOne = CGPointMake(newPositionOne.x/rifleZoom, newPositionOne.y/rifleZoom);
但是,现在无论图像缩放多少,它都可以正常工作。我似乎无法将其实现为从屏幕的中心相对于图像的中心按比例放大图像。
我尝试过的内容
我试图在放大图像之前更改图像的位置。与
newPositionOne
相同,但是这样做不行或操作不正确。编辑
这样可以缩放并将中心带到屏幕中心点,或完全弄乱。袖手旁观有点不足,无法确切地决定它在做什么。
CGPoint positionNow = CGPointMake(backgroundImage.position.x, backgroundImage.position.y);
CGPoint positionPrev = CGPointMake(0.5, 0.5);
float xdiff = positionNow.x - positionPrev.x;
float ydiff = positionNow.y - positionPrev.y;
CGPoint newPositionOne = CGPointMake(0.5- xdiff, 0.5 - ydiff);
newPositionOne = CGPointMake(newPositionOne.x/rifleZoom, newPositionOne.y/rifleZoom);
double xPosition = newPositionOne.x / backgroundImage.size.width;
double yPosition = newPositionOne.y / backgroundImage.size.height;
CGPoint prevAnchorPoint = backgroundImage.anchorPoint;
backgroundImage.anchorPoint = CGPointMake(xPosition,yPosition);
double positionX = backgroundImage.position.x + (backgroundImage.anchorPoint.x - prevAnchorPoint.x) * backgroundImage.size.width;
double positionY = backgroundImage.position.y + (backgroundImage.anchorPoint.y - prevAnchorPoint.y) * backgroundImage.size.height;
backgroundImage.position = CGPointMake(positionX,positionY);
[backgroundImage runAction:[SKAction repeatAction:[SKAction scaleTo:rifleZoom duration:1.0] count:1]];
最佳答案
您可以使用背景节点的锚点属性更改图像缩放的点。默认情况下,anchorPoint
为(0.5,0.5)。这指示节点的中心。如果使anchorPoint(0,0),则将其移到左下角。
anchorPoint:定义精灵中与对应的点
节点的位置。您可以在单位中为此属性指定值
协调空间。默认值为(0.5,0.5),这意味着
精灵以其位置为中心。
当您移动anchorPoint
时,您必须再次调整背景位置以抵消由于更改anchorPoint
而引起的移动。
所以你可以使用
CGPoint xPosition = convertedPoint.x / background.size.width
CGPoint yPosition = convertedPoint.y / background.size.height
CGPoint prevAnchorPoint = background.anchorPoint
background.anchorPoint = CGPointMake(xPosition,yPosition)
CGFloat positionX = background.position.x + (background.anchorPoint.x - prevAnchorPoint.x) * background.size.width
CGFloat positionY = background.position.y + (background.anchorPoint.y - prevAnchorPoint.y) * background.size.height
background.position = CGPointMake(positionX,positionY)