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

时间:2015-08-18 22:24:34

标签: ios objective-c uigesturerecognizer uiswipegesturerecognizer

我一直试图找出如何将一个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];
    }

1 个答案:

答案 0 :(得分:0)

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

UISwipeGestureRecognizer的文档中,它声明:

  

您可以通过调用来确定滑动开始的位置   UIGestureRecognizer方法locationInView:和   locationOfTouch:inView :.前一种方法可以为你提供质心   手势中涉及多个触摸;后者给出了   特定触摸的位置。

例如:对于左侧滑动手势,您将执行类似的操作 首先更改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];
    }
}