在另一个对象中初始化 UISwipeGestureRecognizer 时'action'使用未声明的标识符



我正试图在ViewController的viewDidLoad中创建一个UISvipeGestureRecognizer,它将使用选择器调用另一个对象(MyScene)中的方法。然而,我收到以下错误:

使用未声明的标识符"action">

这是不起作用的原因吗?

样本代码:

@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.view;
if (!skView.scene) {
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:(MyScene) action:@selector(screenSwipedRight)];
swipeRight.numberOfTouchesRequired = 1;
swipeRight.direction=UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:(swipeRight)];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:(MyScene) action:@selector(screenSwipedLeft)];
swipeLeft.numberOfTouchesRequired = 1;
swipeLeft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:(swipeLeft)];
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:(MyScene) action:@selector(screenSwipedUp)];
swipeUp.numberOfTouchesRequired = 1;
swipeUp.direction=UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:(swipeUp)];
}
}

如果需要更多细节,我很乐意提供任何其他信息。

感谢您抽出时间提供帮助!

目标需要是类的实例:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:(MyScene) action:@selector(screenSwipedRight)];

可能需要:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget: scene action:@selector(screenSwipedRight)];

MerhodscreenSwipedRight应该是MyScene类的publia方法。

您的initWithTarget方法都表示initWithTarget:(MyScene)。我想您应该使用self

如果您希望场景对象接收事件,请使用:

UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:scene action:@selector(screenSwipedUp)];

最新更新