如何将 UISwipeGestureRecognizer 与屏幕的一侧相关联?

how do I associate a UISwipeGestureRecognizer with one side of the screen?

我一直在尝试弄清楚如何将一个 UISwipeGestureRecognizer 与屏幕的右半部分相关联,并将另一个 UISwipeGesture 识别器与屏幕的另一半相关联,但是,我未能正确编写此机制的代码。以下是我当前的代码。我不知道如何将其中一个滑动识别器与屏幕的一半相关联。任何帮助将不胜感激

    -(void)didMoveToView:(SKView *)view {
    UISwipeGestureRecognizer *leftSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwiped1)];

[leftSwipe1 setDirection:UISwipeGestureRecognizerDirectionLeft];
[leftSwipe1 setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:leftSwipe1];

UISwipeGestureRecognizer *rightSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwiped1)];

[rightSwipe1 setDirection:UISwipeGestureRecognizerDirectionRight];
[rightSwipe1 setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:rightSwipe1];

self.physicsWorld.gravity = CGVectorMake(0, -9.8);
self.physicsWorld.contactDelegate = self;

   }
    -(void)rightSwiped1 {


SKNode *person1 = [self childNodeWithName:@"person1"];

SKAction *moveRight = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 80, CGRectGetMidY(self.frame) + 200) duration:0.2f];

[person1 runAction:moveRight];

    }
    -(void)leftSwiped1 {
SKNode *person1 = [self childNodeWithName:@"person1"];

SKAction *moveLeft = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 400, CGRectGetMidY(self.frame) + 200) duration:0.2f];

[person1 runAction:moveLeft];
    }

简而言之;您必须自己进行过滤。

UISwipeGestureRecognizer 的文档中指出:

You may determine the location where a swipe began by calling the UIGestureRecognizer methods locationInView: and locationOfTouch:inView:. The former method gives you the centroid if more than one touch was involved in the gesture; the latter gives the location of a particular touch.

例如:对于向左滑动的手势,你会做类似的事情 首先更改您的 initWithTarget 操作选择器以采用这样的参数

UISwipeGestureRecognizer *leftSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwiped1:)];

然后在您的处理程序中执行如下操作:

-(void)leftSwiped1:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint pt = [gestureRecognizer locationInView:self.view];
    if(pt.x < (self.view.bounds.size.width/2))
    {
        SKNode *person1 = [self childNodeWithName:@"person1"];
        SKAction *moveLeft = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 400, CGRectGetMidY(self.frame) + 200) duration:0.2f];
        [person1 runAction:moveLeft];
    }
}