プログラムで作成した UIColletionView に UISwipeGestureRecognizer を追加しようとしましたが、認識機能がアクションを呼び出しません。これが私のコードです。
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.currentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake( 0.0f, 54.0f, 320.0f, 470.0f) collectionViewLayout:flowLayout];
[self.currentCollectionView setBackgroundColor:[UIColor whiteColor]];
self.currentCollectionView.delegate = self;
self.currentCollectionView.dataSource = self;
self.currentCollectionView.showsHorizontalScrollIndicator = NO;
self.currentCollectionView.showsVerticalScrollIndicator = NO;
self.currentCollectionView.scrollEnabled = YES;
self.currentCollectionView.bounces = YES;
[self.currentCollectionView setBackgroundColor:[UIColor lightGrayColor]];
[self.currentCollectionView registerClass:[TripexpPhotoCell class] forCellWithReuseIdentifier:@"photoCell"];
[self.view addSubview:self.currentCollectionView];
self.swipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeUp:)];
self.swipeUpRecognizer.numberOfTouchesRequired = 1;
[self.swipeUpRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];
self.swipeDownRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeDown:)];
self.swipeDownRecognizer.numberOfTouchesRequired = 1;
[self.swipeDownRecognizer setDirection:UISwipeGestureRecognizerDirectionDown];
[self.currentCollectionView addGestureRecognizer:self.swipeDownRecognizer];
[self.currentCollectionView addGestureRecognizer:self.swipeUpRecognizer];
そして、ここに同じ認識エンジンを同時に受け取るための関数とデリゲートがあります
#pragma mark - UISwipeGestureRecognizer Action
-(void)didSwipeUp: (UISwipeGestureRecognizer*) recognizer {
NSLog(@"Swiped Up");
}
-(void)didSwipeDown: (UISwipeGestureRecognizer*) recognizer {
NSLog(@"Swiped Down");
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
NSLog(@"Asking permission");
return YES;
}
また、UICollectionView 内でループし、既存の UISwipeGestureRecognizer があるかどうかを確認しましたが、何も見つかりませんでした。2 つのレコグナイザーをアタッチすると、それらの 2 つが表示されます。