iOS模拟器无法识别手势

问题描述:

我在视图控制器的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

原来的观点是不响应任何手势 - 滚动,点击按钮或滑动操作。我从~/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

首先,你需要添加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

此方法添加到您的视图控制器让你UICollectionView不会阻止其他手势

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

我在控制器的界面上添加了协议,将自己设置为轻击手势识别器的代表并添加上面的方法。没有帮助。 – septerr

重新启动模拟器为我工作。