【问题标题】:CollectionView Cell swipe to perform operationsCollectionView 单元格滑动以执行操作
【发布时间】:2014-06-23 22:02:05
【问题描述】:

嗨,我正在使用 UICollectionview 水平布局开发社交网络应用程序,当我的应用程序启动集合视图时最多加载 5 个单元格,然后如果我滑动第 5 个单元格,那么那时只会分配第 6 个单元格内存。以同样的方式将第 6 个单元格滑动到第 7 个单元格。那么,我该如何实现这个过程

提前感谢您的任何建议

【问题讨论】:

    标签: ios objective-c ios7 uicollectionview uicollectionviewcell


    【解决方案1】:

    您需要向您的 collectionView 添加滑动手势识别器:

    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
    [collectionView addGestureRecognizer:swipeGesture];
    

    然后,在 handleSwipeGesture: 中处理滑动以分配单元格。

    -(void) handleSwipeGesture:(UISwipeGestureRecognizer *) sender
    {
    ...
    }
    

    你可以随意调整滑动方向,这个是向上配置的。

    这就是它的全部内容。主要是您不希望方向与滚动方向滑动冲突,因为我认为没有一种干净的方法来处理它。当您水平滚动时,即为滑动,因此您需要使用向上/向下滑动方向。

    改为附加到单元格(捕捉单个单元格上的手势)我经常使用以下方法为简单的集合视图执行此操作:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        ...
        UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
        [cell addGestureRecognizer:longPressGestureRecognizer];
        ...
    }
    

    (这是一个长按,但它的工作方式相同)。如果将其放入单元格中,您需要在单元格上放置一个标签,然后在处理程序中引用该标签以找出它来自哪个单元格:

    这是上面的一个:

    - (void) handleLongPress: (UILongPressGestureRecognizer *) sender 
    {
        if (sender.state != UIGestureRecognizerStateBegan)
            return;
    
        CGPoint p = [sender locationInView: collectionView];
        NSIndexPath *indexPath = [collectionView indexPathForItemAtPoint:p];
        if (indexPath != nil)
        {
            UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
            if (cell)
        {
        int indexOfItem = cell.tag;
            NSLog(@"Selected item = %d", indexOfItem);
        }
    }
    

    至少沿着这条线...

    【讨论】:

    • 没关系,但是我如何检测滑动精确单元格我的意思是说如果我滑动第 5 个索引单元格,那么那时只会出现新单元格...
    • 最好的方法是将滑动手势识别器附加到单元格上。我将编辑原始答案。
    • 它很有用...但是当我刷第 5 个单元格时,我需要加载第 6 个单元格,这怎么可能...
    • 不应将UIGestureRecognizer 直接附加到CollectionViewCell 上,这可能会干扰必须跟踪的其他手势。见developer.apple.com/library/ios/documentation/WindowsViews/…
    【解决方案2】:

    您可以通过实现 UIScrollViewDelegate 方法来实现这一点

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
          CGPoint offset = scrollView.contentOffset;
          CGRect bounds = scrollView.bounds;
          CGSize size = scrollView.contentSize;
          UIEdgeInsets inset = scrollView.contentInset;
          float y = offset.x + bounds.size.width - inset.right;
          float h = size.width;
    
    
          float reload_distance = 75; //distance for which you want to load more
         if(y > h + reload_distance) {
            // write your code getting the more data
            NSLog(@"load more rows");
    
          }
       }
    

    当您进入集合的最后一个单元格时,将调用此滚动视图委托方法。 您可以根据需要更改距离值。

    【讨论】:

    • 因为你需要在你的视图控制器中实现 UIScrollViewDelegate
    • 你植入收藏视图了吗?
    • 只需像这样实现,例如您的视图控制器名称是 testViewController 然后在 .h 文件中像这样实现 UIScrollViewDelegate 委托 @interface testViewController : GAITrackedViewController 在实现委托后此方法将自动调用 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    相关资源
    最近更新 更多