2014-02-26 23 views
0

在我的iOS應用程序,我有以下設置:UISwipeGestureRecognizer @selector不會被調用,因爲UIPanGestureRecognizer設立

- (void)setupGestures 
{ 
    UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];               
    [self.view addGestureRecognizer:panRecognizer]; 

    UISwipeGestureRecognizer* swipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 

    [swipeUpRecognizer setDirection:UISwipeGestureRecognizerDirectionUp]; 
    [self.view addGestureRecognizer:swipeUpRecognizer]; 
} 

// then I have following implementation of selectors 

// this method supposed to give me the length of the swipe 

- (void)panGesture:(UIPanGestureRecognizer *)sender 
{ 
    if (sender.state == UIGestureRecognizerStateBegan) 
    { 
     startLocation = [sender locationInView:self.view]; 
    } 
    else if (sender.state == UIGestureRecognizerStateEnded) 
    { 
     CGPoint stopLocation = [sender locationInView:self.view]; 
     CGFloat dx = stopLocation.x - startLocation.x; 
     CGFloat dy = stopLocation.y - startLocation.y; 
     CGFloat distance = sqrt(dx*dx + dy*dy); 
     NSLog(@"Distance: %f", distance); 
    } 
} 

// this method does all other actions related to swipes 

- (void)handleSwipe:(UISwipeGestureRecognizer *)gestureRecognizer 
{ 
UISwipeGestureRecognizerDirection direction = [gestureRecognizer direction]; 
    CGPoint touchLocation = [gestureRecognizer locationInView:playerLayerView]; 

    if (direction == UISwipeGestureRecognizerDirectionDown && touchLocation.y > (playerLayerView.frame.size.height * .5)) 
    { 
     if (![toolbar isHidden]) 
     { 
      if (selectedSegmentIndex != UISegmentedControlNoSegment) 
      { 
       [self dismissBottomPanel]; 
      } 
      else 
      { 
      [self dismissToolbar]; 
      } 
     } 
    } 
} 

所以問題是,handleSwipe是從來沒有得到所謂的...當我註釋掉UIPanGestureRecognizer設置,handleSwipe開始工作。

我很新手勢識別編程,所以我假設我在這裏失去了一些基本的東西。

任何形式的幫助,高度讚賞!

+1

附註 - 爲什麼要調用'allocWithZone:nil'?只需調用'alloc'。 – rmaddy

+0

當然......但這並不影響我的問題,對吧? :) –

+0

完全沒有,這就是爲什麼我用「旁註」作爲前綴。 :) – rmaddy

回答

5

你需要告訴手勢如何互相交流。這可以通過讓它們同時運行來完成(默認是不會的),或者通過設置一個只在另一個失敗時才工作。

爲了讓他們都工作,讓您的課delegate的手勢和實施

– gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

返回YES

要設置其中一個僅在另一個失敗時才起作用,請使用requireGestureRecognizerToFail:

+0

謝謝!我只是做了你的建議,但handleSwipe仍然沒有被調用:( –

+0

我添加了UIGestureRecognizerDelegate到我的課,但 - gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:沒有得到調用:( –

+0

我的愚蠢...忘了這樣做:panRecognizer。委託=自我; –

0

揮擊和平移手勢非常相似,引起混亂,

有一些補救措施:

  1. 不要設置平移和輕掃在相同的觀點 - 它可以設置在不同的子視圖上防止混淆。

  2. 使用另一個識別器用於滑動切換等的雙擊或2-手指敲擊因爲這不能被解釋爲鍋

  3. 使用委託方法 - gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer:

    例如如果觸摸開始於面板/工具欄的區域並且不能通過平移識別器允許使用滑動來查找。

相關問題