[英]Limit Pan Gesture with Pinch Gesture
我正在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等),並使用節點/屏幕/等的大小來限制平移。 任何幫助表示贊賞! 謝謝!
好的,這就是我為將來的工作做的參考。
我首先創建了兩個iVar:一個用於節點的初始x位置,另一個用於初始y位置。 然后,我通過以下方法計算出“可移動距離”: sizeOfNode * 0.5 * scale
並減去screenSize * 0.5
。 只要desiredLocation <= (initialPos + movableDistance)
和desiredLocation <= (initialPos + movableDistance)
的位置desiredLocation >= (initialPos - moveableDistance)
您就可以更改節點的位置。 您可以同時針對x和y執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.