我正在将 UISwipeGestureRecognizer 和 UITapGestureRecognizer 添加到视图控制器的 viewDidLoad 方法中的视图中。
- (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。
点击和滑动无法被识别。我是否缺少一些明显的东西? 谢谢。
重新启动模拟器对我有用。
结果视图没有响应任何手势 - 滚动、点击按钮或滑动操作。我从
~/Library/Application Support/iPhone Simulator / 6.1/Applications
和 ~/Library/Developer/Xcode/DerivedData
删除了生成的文件夹,重置模拟器设置(从 iOS Simulator
> Reset Contents and Settings
),在 xcode 中进行清理(产品 > 清理)并再次运行应用程序。现在可以识别手势了。我不确定以上哪一项解决了问题...可能只需重置模拟器的内容和设置就足够了。
将此方法添加到您的视图控制器中,以便您的 UICollectionView 不会阻止其他手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return true;
}
如果您已经从窗口面板启用了设备边框,但仍然无法使用滑动返回手势。请参考这个答案。
您需要做的就是重新启动模拟器并尝试从模拟器的真实边缘滑动。
确保您按下触控板来启动手势。从 Apple 开发论坛粘贴。
这就是我在不起作用时所做的:
这就是我在工作时所做的事情:
首先您需要将 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
}