我正在开发一项功能,该功能将允许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 例外预处理+ 180
1个libobjc.A.dylib 0x015e18b6 objc_exception_throw + 44
2 CoreFoundation 0x018fb903-[NSObject(NSObject)didNotRecognizeSelector:] + 275
3 CoreFoundation 0x0184e90b __forwarding + 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图形服务0x038039e2 GSEventRunModal + 192
21 GraphicsServices 0x03803809 GSEventRun + 104
22 UIKit 0x00230d3b UIApplicationMain + 1225
23 Romp 0x0000615d主+ 141
24 libdyld.dylib 0x01e9c70d开始+ 1
25 ??? 0x00000001 0x0 +1
)
libc ++ abi.dylib:以NSException类型的未捕获异常终止
(lldb)
最佳答案
'-[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将向您显示代码行。从调试中消除了很多猜测。
关于ios - 基于位置的UIGestureRecognizers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20927480/