gpt4 book ai didi

ios - 如何在 Sprite Kit 中执行没有触摸 Action 的平移?

转载 作者:行者123 更新时间:2023-11-29 10:23:23 25 4
gpt4 key购买 nike

touchesBegan 中,我有单点触摸的逻辑。而且我正在尝试添加使用平移更改相机位置的功能。对于平底锅,我使用 touchesMoved。一切都很好,但是一旦我平移,触摸 Action 也会被执行。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];

NSArray *sprites = [self nodesAtPoint:location];
for (SKSpriteNode *sprite in sprites)
{
//*
//* How to stop executing this block when panning?
//*
}

}
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
CGPoint previousPosition = [touch previousLocationInNode:self];
CGPoint translation = CGPointMake((-1)*(positionInScene.x - previousPosition.x), (-1)*(positionInScene.y - previousPosition.y));
CGPoint cameraPos = [self camera].position;

[self camera].position = CGPointAdd(cameraPos, translation);

}

最佳答案

查看如何使用 IOS 内置的 Pan Gesture,使用它您将不得不选择是否允许它也执行触摸事件。

我会使用 View Controller 给你一个答案,不过你可以在其他地方使用它

Objective-C :

打开 ViewController.h 并添加以下声明:

@interface ViewController : UIViewController<UIGestureRecognizerDelegate>

...

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer;

然后在ViewController.m中实现如下:

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {

CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}

http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more

此时你可以像上面的教程一样通过 UI 链接它,或者在开头的某个地方声明

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.view addGestureRecognizer:pan];

swift :

class ViewController : UIViewController, UIGestureRecognizerDelegate

...然后在你的代码中

@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translationInView(self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPointZero, inView: self.view)
}

http://www.raywenderlich.com/76020/using-uigesturerecognizer-with-swift-tutorial

此时你可以像上面的教程一样通过 UI 链接它,或者像 init 一样在开始阶段的某个地方声明:

let pan = UIPanGestureRecognizer(target: self, action: "handlePan:")
self.view.addGestureRecognizer(pan);

关于ios - 如何在 Sprite Kit 中执行没有触摸 Action 的平移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33648952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com