在UICollectionView中检测滑动

9

当用户滑动uicollectionview时,我需要执行特定的操作。我的设计是每个单元格占据整个屏幕。

我尝试了以下两种方法:

A. scrollViewDidEndDecelerating

# pragma UIScrollView
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"detecting scroll");
    for (UICollectionViewCell *cell in [_servingTimesCollectionView visibleCells]) {
        NSIndexPath *indexPath = [_servingTimesCollectionView indexPathForCell:cell];
        CGPoint scrollVelocity = [scrollView.panGestureRecognizer velocityInView:_servingTimesCollectionView];
        if (scrollVelocity.x > 0.0f)
            NSLog(@"going right");
        else if (scrollVelocity.x < 0.0f)
            NSLog(@"going left");
    }
}

但是scrollVelocity返回null。该方法已被调用。

B. UISwipeGestureRecognizer

在我的UIViewControllerViewDidLoad中,我添加了以下内容,它同时代理了UICollectionViewDataSourceUIGestureRecognizerDelegate

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeRight:)];
swipeRight.numberOfTouchesRequired = 1;
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeLeft:)];
swipeRight.numberOfTouchesRequired = 1;
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

[_servingTimesCollectionView addGestureRecognizer:swipeRight];
[_servingTimesCollectionView addGestureRecognizer:swipeLeft];

还有在UiViewController中的以下内容:

#pragma mark - UISwipeGestureRecognizer Action
-(void)didSwipeRight: (UISwipeGestureRecognizer*) recognizer {
    NSLog(@"Swiped Right");
}

-(void)didSwipeLeft: (UISwipeGestureRecognizer*) recognizer {
    NSLog(@"Swiped Left");
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer     shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    NSLog(@"Asking permission");
    return YES;
}

但是没有一个被调用。

问题出在哪里?我的开发目标是iOS7。

2个回答

11

您没有设置手势的代理:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeRight:)];
    swipeRight.delegate = self;
    swipeRight.numberOfTouchesRequired = 1;
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeLeft:)];
    swipeLeft.delegate = self;
    swipeLeft.numberOfTouchesRequired = 1;
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];

1
我试图和你做同样的事情(通过在UICollectionView中使用全尺寸单元格框架)。仅仅委托自己是不够的,因为UICollectionView可能有它自己的手势识别器。所以以下技巧在我的情况下起作用了。
    override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.backgroundColor = .red

    let leftGR = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeHandler_Left(gestureRecognizer:)))
    leftGR.numberOfTouchesRequired = 1
    leftGR.direction = .left
    leftGR.delegate = self

    self.view.addGestureRecognizer(leftGR)
}

@objc func swipeHandler_Left(gestureRecognizer : UISwipeGestureRecognizer!) {
    if gestureRecognizer.state == .ended {
        // Perform action.
        print("***Free to Delete Old Cell")
    }
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

抱歉我回答得很快,请尝试翻译Objective-C :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接