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。
水龙头和水龙头没有得到承认。有什么明显的我失踪了? 谢谢。
原来的观点是不响应任何手势 - 滚动,点击按钮或滑动操作。我从~/Library/Application Support/iPhone Simulator/6.1/Applications
和~/Library/Developer/Xcode/DerivedData
中删除生成的文件夹,重置模拟器设置(从iOS Simulator
>Reset Contents and Settings
),在xcode(Product> Clean)中进行了清理并再次运行该应用程序。手势现在已被识别。我不确定上述哪一项可以解决问题......只需重新设置模拟器的内容和设置就足够了。
首先,你需要添加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
}
你不需要添加'UIGestureRecognizerDelegate'。这是可选的。如果您需要实施委托方法,则只需进行设置。并没有'UITapGestureRecognizerDelegate'这样的东西。 – rmaddy
此方法添加到您的视图控制器让你UICollectionView不会阻止其他手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return true;
}
我在控制器的界面上添加了
重新启动模拟器为我工作。
你有没有检查过这些方法被调用或不... ...? –
请阅读'UIGestureRecognizer'的文档。具体来说就是在动作方法中检查手势识别器的'state'属性。 – rmaddy
如果您的集合视图覆盖整个'self.view',那么'self.view'的手势识别器可能永远不会获得任何事件,因为集合视图将处理它们。 – rmaddy