2013-05-22 140 views
0

我在視圖控制器的viewDidLoad方法中添加了UISwipeGestureRecognizer和UITapGestureRecognizer視圖。iOS模擬器無法識別手勢

- (void)viewDidLoad { 
     [super viewDidLoad]; 
     [self.view addGestureRecognizer:[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cardSwipe:)]]; 
     [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cardTap:)]]; 
    } 
- (void)cardSwipe:(UISwipeGestureRecognizer *)sender { 
    //get the card. set faceUp to false. 
    CGPoint location = [sender locationInView:sender.view]; 
    NSIndexPath *cellIndex = [self.cardCollectionView indexPathForItemAtPoint:location]; 
    if(cellIndex){ 
     UICollectionViewCell *cell = [self collectionView:self.cardCollectionView cellForItemAtIndexPath:cellIndex]; 
     if(cell && [cell isKindOfClass:[CardCollectionViewCell class]]){ 
      [[((CardCollectionViewCell *)cell) cardView] handleCardSwipe]; 
     } 
    } 
} 
- (void)cardTap:(UITapGestureRecognizer *)sender { 
    //get the card. set faceUp to false. 
    CGPoint location = [sender locationInView:sender.view]; 
    NSIndexPath *cellIndex = [self.cardCollectionView indexPathForItemAtPoint:location]; 
    if(cellIndex){ 
     UICollectionViewCell *cell = [self collectionView:self.cardCollectionView cellForItemAtIndexPath:cellIndex]; 
     if(cell && [cell isKindOfClass:[CardCollectionViewCell class]]){ 
      [[((CardCollectionViewCell *)cell) cardView] handleCardSwipe]; 
     } 
    } 
} 

如果這是相關的:視圖包含UICollectionView。

水龍頭和水龍頭沒有得到承認。有什麼明顯的我失蹤了? 謝謝。

+0

你有沒有檢查過這些方法被調用或不... ...? –

+0

請閱讀'UIGestureRecognizer'的文檔。具體來說就是在動作方法中檢查手勢識別器的'state'屬性。 – rmaddy

+0

如果您的集合視圖覆蓋整個'self.view',那麼'self.view'的手勢識別器可能永遠不會獲得任何事件,因爲集合視圖將處理它們。 – rmaddy

回答

1

原來的觀點是不響應任何手勢 - 滾動,點擊按鈕或滑動操作。我從~/Library/Application Support/iPhone Simulator/6.1/Applications~/Library/Developer/Xcode/DerivedData中刪除生成的文件夾,重置模擬器設置(從iOS Simulator>Reset Contents and Settings),在xcode(Product> Clean)中進行了清理並再次運行該應用程序。手勢現在已被識別。我不確定上述哪一項可以解決問題......只需重新設置模擬器的內容和設置就足夠了。

+0

這是一個與代碼無關的合法問題,儘管我無法提供有關原因的解釋。當我遇到它時,我所做的只是重置模擬器並重新部署應用程序,然後再次開始工作。 – Robert

+0

我在做模擬器重置後有這個問題,但只在iPhone 6模擬器上。 – Brett

-3

首先,你需要添加UITapGestureRecognizer委託方法要的.h

@interface ViewController : UIViewController<UIGestureRecognizerDelegate> 

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapImgView:)]; 
     doubleTap.numberOfTapsRequired = 2; 
     doubleTap.delegate = self; 

- (void)doubleTapImgView:(UITapGestureRecognizer *)gesture 
{ 
    //Do What you want Here 
} 
+1

你不需要添加'UIGestureRecognizerDelegate'。這是可選的。如果您需要實施委託方法,則只需進行設置。並沒有'UITapGestureRecognizerDelegate'這樣的東西。 – rmaddy

0

此方法添加到您的視圖控制器讓你UICollectionView不會阻止其他手勢

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

我在控制器的界面上添加了協議,將自己設置爲輕擊手勢識別器的代表並添加上面的方法。沒有幫助。 – septerr

2

重新啓動模擬器爲我工作。