2016-07-27 68 views
1

我正在製作一個遊戲,玩家一次移動兩個數字。每個人都有自己的一半屏幕,並在內部移動。不幸的是,我發現,當我用拇指滑動時,什麼都沒有發生。甚至連我的一個識別器都沒有被觸發。模擬獨立手勢

也許有一種方法。我在GameViewController的頂部創建了另外兩個視圖並添加了單獨的手勢。但我無法在我的gamescene.m中向他們作出反應來創建動作。

是否有無論如何承認在GameViewControl中GameScene中聲明的滑動,並添加到他們的任何操作?

我已經試圖根據觸摸開始並結束我自己的識別器,但是當兩個手指一次被釋放時它變得混亂並且通常忘記反應兩次,我的意思是分別爲每個版本。

-(void)setUpGestActions{ 
    _swipeGestureLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft:)]; 
    [self.swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 
    self.swipeGestureLeft.cancelsTouchesInView = NO; 
    self.swipeGestureLeft.delegate = self; 
    [self.view addGestureRecognizer: self.swipeGestureLeft]; 

    _swipeGestureRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight:)]; 
    [self.swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight]; 
    self.swipeGestureRight.cancelsTouchesInView = NO; 
    self.swipeGestureRight.delegate = self; 
    [self.view addGestureRecognizer: self.swipeGestureRight]; 

    _swipeGestureUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeUp:)]; 
    [self.swipeGestureUp setDirection:UISwipeGestureRecognizerDirectionUp]; 
    self.swipeGestureUp.cancelsTouchesInView = NO; 
    self.swipeGestureUp.delegate = self; 
    [self.view addGestureRecognizer: self.swipeGestureUp]; 

    _swipeGestureDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDown:)]; 
    [self.swipeGestureDown setDirection:UISwipeGestureRecognizerDirectionDown]; 
    self.swipeGestureDown.cancelsTouchesInView = NO; 
    self.swipeGestureDown.delegate = self; 
    [self.view addGestureRecognizer: self.swipeGestureDown]; 

    _moveLeft = [SKAction moveByX:-self.frame.size.width/6 y:0 duration:self.velocity]; 
    _moveRight = [SKAction moveByX:self.frame.size.width/6 y:0 duration:self.velocity]; 
    _moveUp = [SKAction moveByX:0 y:self.frame.size.width/6 duration:self.velocity]; 
    _moveDown = [SKAction moveByX:0 y:-self.frame.size.width/6 duration:self.velocity]; 

    _downMovement = [SKAction moveByX:0 y:-1 duration:self.downMovementVelocity]; 
} 

-(void)swipeLeft:(UISwipeGestureRecognizer*) recognizer{ 
    _sideDisting = [recognizer locationInView:self.view]; 
    if(self.sideDisting.x <= self.frame.size.width/2){ 
     [_boy runAction:self.moveLeft]; 
    } 
    else{ 
     [_girl runAction:self.moveLeft]; 

    } 
} 

-(void)swipeRight:(UISwipeGestureRecognizer*) recognizer{ 
    _sideDisting = [recognizer locationInView:self.view]; 
    if(self.sideDisting.x <= self.frame.size.width/2){ 
     [_boy runAction:self.moveRight]; 
    } 
    else{ 
     [_girl runAction:self.moveRight]; 
    } 
} 

-(void)swipeUp:(UISwipeGestureRecognizer*) recognizer{ 
    _sideDisting = [recognizer locationInView:self.view]; 
    if(self.sideDisting.x <= self.frame.size.width/2){ 
     [_boy runAction:self.moveUp]; 
    } 
    else{ 
     [_girl runAction:self.moveUp]; 
    } 
} 

-(void)swipeDown:(UISwipeGestureRecognizer*) recognizer{ 
    _sideDisting = [recognizer locationInView:self.view]; 
    if(self.sideDisting.x <= self.frame.size.width/2){ 
     [_boy runAction:self.moveDown]; 
    } 
    else{ 
     [_girl runAction:self.moveDown]; 

    } 
} 

回答

0

最簡單的解決方案是將屏幕分爲兩個較小的觀點和附加單獨的手勢識別到每一個。

1

要在同一時間識別多個手勢,請在每個手勢識別器上設置一個委託。代表可以是每個手勢的相同對象。

在委託,實施這樣的:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
    shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 
    return YES; 
} 
+0

我已經這樣做了,但沒有任何結果。將interface和bool中的delegate設置爲YES,您也可以看到self.swipeGestureDown.delegate = self;在每個分配代碼塊中行。我認爲它與其他一些識別器一樣,比如雙擊或其他。 (我沒有在我的代碼中聲明這樣的事情) –

0

我覺得你可以看看下面這個例子。 它可能會幫助你。

UIScreenEdgePanGestureRecognizer *myScreenEdgePanGestureRecognizer; 
... 
myScreenEdgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScreenEdgePan:)]; 
myScreenEdgePanGestureRecognizer.delegate = self; 
// Configure the gesture recognizer and attach it to the view. 
... 
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 
    BOOL result = NO; 
    if ((gestureRecognizer == myScreenEdgePanGestureRecognizer) && [[otherGestureRecognizer view] isDescendantOfView:[gestureRecognizer view]]) { 
     result = YES; 
    } 
    return result; 
} 

通過此鏈接,你會發現更多的信息。

https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html