我正在开发一项功能,允许iOS用户滑动屏幕的左半部分并触发方法。 UISwipeGestureRecognizers在ViewController.m文件中调用,这些识别器选择的方法在MyScene.m中。
我可以在没有滑动位置设置的情况下使用它,但我还没有找到一种成功的方法,只能在屏幕的左半部分触发它。我在下面列出了一些代码,显示了有和没有位置要求的示例。
ViewController.m:
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
SKView * skView = (SKView *)self.view;
if (!skView.scene) {
skView.showsFPS = YES;
skView.showsNodeCount = YES;
SKScene *scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:scene];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:(scene) action:@selector(screenSwipedRight)];
swipeRight.numberOfTouchesRequired = 1;
swipeRight.direction=UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:(swipeRight)];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:(scene) action:@selector(screenSwipedLeft)];
swipeLeft.numberOfTouchesRequired = 1;
swipeLeft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:(swipeLeft)];
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:(scene) action:@selector(screenSwipedUp)];
swipeUp.numberOfTouchesRequired = 1;
swipeUp.direction=UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:(swipeUp)];
}
}
MyScene.m:
-(void)screenSwipedRight:(UISwipeGestureRecognizer *)swipedRight
{
CGPoint touchPoint = [swipedRight locationInView:(self.view)];
NSInteger movePlayer;
if (touchPoint.x <=self.view.bounds.size.width/2) {
movePlayer = 1;
} else {
movePlayer = 2;
}
if (movePlayer ==1) {
CGFloat percentToRight = 1-(self.playerOne.position.x / self.view.bounds.size.width);
NSTimeInterval timeToRight = self.horizontalRunSpeed * percentToRight;
NSLog(@"Percent to right = %f",percentToRight);
NSLog(@"Time to right = %f",timeToRight);
SKAction *moveNodeRight = [SKAction moveToX:self.view.bounds.size.width-self.playerOne.size.width duration:timeToRight];
[self.playerOne runAction:[SKAction sequence:@[moveNodeRight]]];
}
}
-(void)screenSwipedLeft
{
NSLog(@"Screen was swiped to the left");
CGFloat percentToLeft = self.playerOne.position.x / self.view.bounds.size.width;
NSTimeInterval timeToLeft = self.horizontalRunSpeed * percentToLeft;
NSLog(@"Percent to left = %f",percentToLeft);
NSLog(@"Time to left = %f",timeToLeft);
SKAction *moveNodeLeft = [SKAction moveToX:self.playerOne.size.width duration:timeToLeft];
[self.playerOne runAction:[SKAction sequence:@[moveNodeLeft]]];
}
-(void)screenSwipedUp
{
NSLog(@"Screen was swiped up");
[self runAction:[SKAction sequence:@[[SKAction runBlock:^{ self.physicsWorld.gravity = self.antiGravityAmount;}],[SKAction waitForDuration:.25],[SKAction runBlock:^{ self.physicsWorld.gravity = self.gravityAmount;}]]]];
}
swipedRight方法是我尝试区分滑动位置的地方。
执行此代码会导致SIGABRT信号错误,但只有当我向右滑动时才会出现错误。向左或向上滑动可继续正常工作。它可能与“locationInView”中的视图选择有关,但我可能完全错了。
有谁知道如何使这项工作?
感谢您的帮助。
编辑:这是错误的日志文件 -
2014-01-04 18:10:32.316 Romp [584:70b]无法找到CFBundle 0x976afe0的可执行文件(未加载)
2014-01-04 18:10:32.375 Romp [584:70b] viewDidLoad已经运行
2014-01-04 18:10:32.425 Romp [584:70b]尺码:{480,320}
2014-01-04 18:10:32.427 Romp [584:70b]屏幕初始化
2014-01-04 18:10:33.917 Romp [584:70b] - [MyScene screenSwipedRight]:无法识别的选择器发送到实例0x9995790
2014-01-04 18:10:33.920 Romp [584:70b] * 由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [MyScene screenSwipedRight]:无法识别的选择器发送到实例0x9995790'
* 第一次抛出调用堆栈:
( 0 CoreFoundation 0x0185e5e4 exceptionPreprocess + 180 1 libobjc.A.dylib 0x015e18b6 objc_exception_throw + 44 2 CoreFoundation 0x018fb903 - [NSObject(NSObject)doesNotRecognizeSelector:] + 275 3 CoreFoundation 0x0184e90b __ 转发 + 1019 4 CoreFoundation 0x0184e4ee _CF_forwarding_prep_0 + 14 5 UIKit 0x005a8e8c _UIGestureRecognizerSendActions + 230 6 UIKit 0x005a7b00 - [UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 383 7 UIKit 0x005a956d - [UIGestureRecognizer _delayedUpdateGesture] + 60 8 UIKit 0x005acacd _UIGestureRecognizerUpdate_block_invoke + 57 9 UIKit 0x005aca4e _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 317 10 UIKit 0x005a3148 _UIGestureRecognizerUpdate + 199 11 UIKit 0x0026f19a - [UIWindow _sendGesturesForEvent:] + 1291 12 UIKit 0x002700ba - [UIWindow sendEvent:] + 1030 13 UIKit 0x00243e86 - [UIApplication sendEvent:] + 242 14 UIKit 0x0022e18f _UIApplicationHandleEventQueue + 11421 15 CoreFoundation 0x017e783f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 15 16 CoreFoundation 0x017e71cb __CFRunLoopDoSources0 + 235 17 CoreFoundation 0x0180429e __CFRunLoopRun + 910 18 CoreFoundation 0x01803ac3 CFRunLoopRunSpecific + 467 19 CoreFoundation 0x018038db CFRunLoopRunInMode + 123 20 GraphicsServices 0x038039e2 GSEventRunModal + 192 21 GraphicsServices 0x03803809 GSEventRun + 104 22 UIKit 0x00230d3b UIApplicationMain + 1225 23 Romp 0x0000615d main + 141 24 libdyld.dylib 0x01e9c70d start + 1 25 ??? 0x00000001 0x0 + 1 )
libc ++ abi.dylib:以NSException类型的未捕获异常终止 (lldb)
答案 0 :(得分:2)
'-[MyScene screenSwipedRight]: unrecognized selector sent to instance 0x9995790'
漂亮的标准错误。你这样做:
UISwipeGestureRecognizer *swipeRight =
[[UISwipeGestureRecognizer alloc] initWithTarget:(scene)
action:@selector(screenSwipedRight)];
您注册了@selector(screenSwipedRight)
作为回调,这是一种不带参数的方法。您的代码中不存在此方法。
您的方法具有相同的名称,但它需要额外的UISwipeGestureRecognizer*
参数,这使得它成为一种不同的方法。因此,您需要将其注册为@selector(screenSwipedRight:)
。注意结尾的冒号。
PS:在您的应用中启用exception breakpoint。这将使Xcode在出现像这样的异常时显示代码行。调试时需要进行大量猜测。