我创建了一个 iOS 应用程序,我需要能够在其中同时移动、旋转和缩放 Sprite (我使用的是 Apple 的 Sprite Kit)。在大多数情况下,我有这个工作。我目前可以用一根手指触摸并移动 Sprite ,如果我用两根手指,我可以缩放和旋转 Sprite 。为此,我使用了 UIPanGestureRecognizer、UIPinchGestureRecognizer 和 UIRotateGestureRecognizer。那很好用。我想要的是,当我用右手拖动、旋转和缩放一个 Sprite 时,我可以用我的左手独立于另一个 Sprite 拖动、旋转和缩放一个不同的 Sprite 。
目前我正在使用 iOS 手势来移动、旋转和缩放 Sprite 。我使用的代码与我在 Ray Wenderlich 网站上的 Sprite Kit 拖放 Sprites 教程中找到的代码非常接近。 http://www.raywenderlich.com/44270/sprite-kit-tutorial-how-to-drag-and-drop-sprites .当他开始使用 UIPanGestureRecognizers 而不仅仅是触摸方法时,我正在使用的部分靠近底部。
就像我说的那样,手势一次可以在一个 Sprite 上正常工作。我如何让它适用于多个 Sprite ?
例如,对于 UIPanGesturRecognizer,我添加了以下代码:
- (void)didMoveToView:(SKView *)view {
UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
[[self view] addGestureRecognizer:gestureRecognizer];
然后我在下面有一个名为 gestureRecognizer 的方法:
- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
CGPoint touchLocation = [recognizer locationInView:recognizer.view];
touchLocation = [self convertPointFromView:touchLocation];
[self selectNodeForTouch:touchLocation]; // This just returns the node that has been touched
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:recognizer.view];
translation = CGPointMake(translation.x, -translation.y);
[self panForTranslation:translation];
[recognizer setTranslation:CGPointZero inView:recognizer.view];
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
[_selectedNode removeAllActions];
}
最后是移动 Sprite 的方法:
- (void)panForTranslation:(CGPoint)translation {
if([[_selectedNode name] isEqualToString:kAnimalNodeName]) {
CGPoint position = [_selectedNode position];
// Set variable for the point to move selected node
CGPoint movePoint = CGPointMake(position.x + translation.x, position.y + translation.y);
[_selectedNode setPosition:newPos];
}
现在示例代码只显示了 UIPanGestureRecognizer 的方法,但我也有类似的旋转和捏合手势方法。所有这些代码都在我的场景类中。
谢谢你的帮助。
最佳答案
好吧,你发布的教程几乎展示了如何去做...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
[self selectNodeForTouch:positionInScene];
}
- (void)selectNodeForTouch:(CGPoint)touchLocation {
//The below statement assigns touchedNode from a sprite that contains touchLocation
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation];
//2
if(![_selectedNode isEqual:touchedNode]) {
[_selectedNode removeAllActions];
[_selectedNode runAction:[SKAction rotateToAngle:0.0f duration:0.1]];
_selectedNode = touchedNode;
//the below if statement determines what SKNode is to be given a SKAction
if([[touchedNode name] isEqualToString:kAnimalNodeName]) {
SKAction *sequence = [SKAction sequence:@[[SKAction rotateByAngle:degToRad(-4.0f) duration:0.1],
[SKAction rotateByAngle:0.0 duration:0.1],
[SKAction rotateByAngle:degToRad(4.0f) duration:0.1]]];
[_selectedNode runAction:[SKAction repeatActionForever:sequence]];
}
}
}
因此,如果您想将操作应用于多个节点,只需为它们指定相同的名称即可。我建议命名您的节点,将它们放在一个数组中,然后遍历它们以检查它们是否具有相同的名称。 如果您有任何其他问题,请发表评论。
更新:1
- (void)panForTranslation:(CGPoint)translation {
//Once again you would do the same thing. Just give the nodes the same name.
if([[_selectedNode name] isEqualToString:kAnimalNodeName]) {
CGPoint position = [_selectedNode position];
// Set variable for the point to move selected nodes
CGPoint movePoint = CGPointMake(position.x + translation.x, position.y + translation.y);
[_selectedNode setPosition:newPos];
}
关于ios - 使用 SpriteKit 手势同时分别拖动、旋转和缩放多个 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22826246/