检测UICollectionView中的滑动

时间:2013-10-07 18:58:42

标签: iphone ios objective-c uicollectionview uiswipegesturerecognizer

当用户滑动uicollectionview时,我需要执行特定操作。 我以每个单元格捕获全屏的方式构建它。

我试过这些方法:

:一种。 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

在我ViewDidLoad的{​​{1}}代理UIViewControllerUICollectionViewDataSource我添加了:

UIGestureRecognizerDelegate

以及UiViewController中的以下内容:

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

但没有人被召唤。

有什么问题?我正在为ios7开发

2 个答案:

答案 0 :(得分:9)

您没有设置手势的代表:

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 :(得分:0)

我试图与你做同样的事情(通过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
}

对不起我的回答,请尝试翻译objc:)