繁体   English   中英

检测UICollectionView中的滑动

[英]Detect swiping in UICollectionView

我需要在用户滑动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

在我的UIViewController ViewDidLoad ,它委托给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开发

您没有设置手势的代理:

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

我试图与你做同样的事情(通过UICollectionView中的全尺寸单元格框架)。 仅使用self委托是不够的,因为UICollectionView可能拥有它自己的手势识别器。 所以下面的伎俩在5个案例中有效。

    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 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM