gpt4 book ai didi

objective-c - UICollectionView 上的 UISwipeGestureRecognizer 不起作用

转载 作者:行者123 更新时间:2023-11-28 20:27:52 25 4
gpt4 key购买 nike

我尝试将 UISwipeGestureRecognizer 添加到我以编程方式创建的 UIColletionView,但识别器从未调用该操作。这是我的代码。

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

这里是同时接收相同识别器的函数和委托(delegate)

#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 个。

最佳答案

我知道这是一篇旧帖子,但这可能会对某些人有所帮助,所以我在这里给出我的解决方案。

UICollectionView 继承自 UIScrollView。您需要先禁用滚动

self.currentCollectionView.scrollEnabled = FALSE;

关于objective-c - UICollectionView 上的 UISwipeGestureRecognizer 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13367254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com