我正在创建一个游戏,在Spritekit,并使用潘手势与捏手势,以查看地图。目前,我限制捏手势的缩放到2.0的比例。我试图把潘的手势限制在屏幕的边界上,即使在缩放时也是如此。地图比屏幕大,我只希望用户能够在地图的外部边缘到达屏幕的外部边缘之前,甚至在缩放时也是如此。这里是我的业余方法,试图处理这种情况:
-(void) handlePanFrom:(UIPanGestureRecognizer*)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
[recognizer setTranslation:CGPointZero inView:recognizer.view];
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:recognizer.view];
translation = CGPointMake(-translation.x, translation.y);
CGPoint desiredPos = CGPointSubtract(_mapNode.position, translation);
NSLog(@"Moving map to x: %f y: %f", desiredPos.x, desiredPos.y);
NSLog(@"Map node position x: %f y: %f", _mapNode.position.x, _mapNode.position.y);
NSLog(@"Map scale is %f", mapScale);
NSLog(@"Map size is x: %f y: %f", _mapNode.map.frame.size.width, _mapNode.map.frame.size.height);
if (desiredPos.y <= (mapScale * 300) && desiredPos.y >= ((1/mapScale) * 200)) {
_mapNode.position = CGPointMake(_mapNode.position.x, desiredPos.y);
[recognizer setTranslation:CGPointZero inView:recognizer.view];
}
if (desiredPos.x <= (mapScale * 250) && desiredPos.x >= ((1/mapScale) * 77)) {
_mapNode.position = CGPointMake(desiredPos.x, _mapNode.position.y);
[recognizer setTranslation:CGPointZero inView:recognizer.view];
}
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
}
}当缩放以获得节点的缩放时,我设置了一个标度iVar。我应该能够使用节点的大小(_mapNode.map是一个SKSpriteNode)和屏幕大小来获得我想要的摇摄。
我以为我可以做scale*xMax和(1/scale)*xMin (也有y位置),但这似乎行不通。我希望那里没有硬数字(300、200等),并使用节点/屏幕/etc的大小来限制摇摄。任何帮助都是非常感谢的!谢谢!
发布于 2014-05-29 21:54:48
好吧,这就是我为将来的人做的参考。
我首先创建了两个iVars:一个用于节点的初始x位置,另一个用于初始y位置。然后,通过取:sizeOfNode * 0.5 * scale和减去screenSize * 0.5,计算出“可移动距离”。只要是desiredLocation <= (initialPos + movableDistance)和desiredLocation >= (initialPos - moveableDistance),就可以更改节点的位置。你可以对x和y都这样做。
https://stackoverflow.com/questions/23899602
复制相似问题