iOS Simulator Not Recognizing Gestures

柔情痞子 提交于 2020-06-01 07:15:06

问题


I am adding a UISwipeGestureRecognizer and a UITapGestureRecognizer to a view in a view controller's viewDidLoad method.

- (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];
        }
    }
}

In case this is relevant: The view contains a UICollectionView.

The taps and swipes are not getting recognized. Is there something obvious that I am missing? Thanks.


回答1:


Restarting the simulator worked for me.




回答2:


Turns out the view was not responding to any gestures - scrolling, taps on buttons or the swipe actions. I deleted generated folders from ~/Library/Application Support/iPhone Simulator / 6.1/Applications and ~/Library/Developer/Xcode/DerivedData, reset the simulator settings (from iOS Simulator > Reset Contents and Settings), did a clean in xcode (Product > Clean) and ran the app again. The gestures are now recognized. I am not sure which of the above fixed the problem...it is possible that simply resetting the simulator's contents and settings would have been enough.




回答3:


You just need to select Show Device Bezels:

Goto simulator > Window > Enable Show Device Bezels

Enjoy your swipe to back gesture.




回答4:


add this method to your viewcontroller so your UICollectionView doesn't block other gestures

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



回答5:


First you Need To Add UITapGestureRecognizer Delegate method To .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
}


来源:https://stackoverflow.com/questions/16683688/ios-simulator-not-recognizing-gestures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!