3

我目前已经创建了一组图像 [1 - 8]。我还创建了一个集合视图,它从 imageArray 中提取图像并将图像放入单元格中的 imageview 中。请注意,该单元格中有一个 imageView,它占据了整个屏幕,水平滚动和分页都启用了。

现在使用我拥有的当前代码,目前正在发生的事情非常奇怪,所以我会告诉你目前正在发生的事情。那么当前正在发生的事情(我将通过图像 1 索引 0)如果图像在 2(索引 1)上,然后您在 3(索引 2)旁边滑动,它会跳过图像 3(索引 2)和 4(索引 3)并位于图像 5(索引 4)上,所以当我的意思是跳过时,我的意思是它滑过您刚刚滑动到的图像,然后再滑过一个。

(奇怪的是它在 5 上删除了图像 1 和 2)一次。我相信这是由于它正在更新索引或将其再次设置为 2,因为它刚刚删除了 0。我知道这可能很难得到但只是想想带有分页的滚动视图,当您滑动到第三张图像时,它会跳过您打开的一张和另一张,然后将您滑动到它所在位置的图像 5。

到目前为止,感谢你们中的一些人,我在下面提出了这段代码,但我希望有人能够解决这个可怕的混乱。

 var currentImage = 0
   func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let x = floor(myCollectionView.contentOffset.x / view.frame.width)
    if Int(x) != currentImage {
        currentImage = Int(x)
    }
    if currentImage > 1 {
    for collectionCell in myCollectionView.visibleCells  as [UICollectionViewCell]  {
        let visibleCells = myCollectionView.visibleCells
        if visibleCells.first != nil {
            if let indexPath = myCollectionView.indexPath(for: collectionCell as UICollectionViewCell) {
                let indexPathOfLastItem = (indexPath.item) - 1
                let indexPathOfItemToDelete = IndexPath(item: (indexPathOfLastItem), section: 0)
                imageArray.remove(at: (indexPath.item) - 1)
                myCollectionView.deleteItems(at: [indexPathOfItemToDelete])
        }
        }
        }
    }
}
4

2 回答 2

4

这些代码将使用项目索引删除特定项目。这很好用!

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    var visibleRect = CGRect()

    visibleRect.origin = myCollectionView.contentOffset
    visibleRect.size = myCollectionView.bounds.size

    let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
    let visibleIndexPath: IndexPath = myCollectionView.indexPathForItem(at: visiblePoint)!

    print(visibleIndexPath)

    fruitArray.remove(at: visibleIndexPath.row )
    myCollectionView.deleteItems(at: [visibleIndexPath])
    myCollectionView.scrollToItem(at: IndexPath.init(row: visibleIndexPath.row-1, section: 0), at: UICollectionViewScrollPosition.centeredHorizontally, animated: false)
}
于 2017-03-28T13:17:48.677 回答
0

使用 scrollViewDidEndDragging 来检测用户何时停止滚动并删除单元格,..

目标 C

 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
        // if decelerating, let scrollViewDidEndDecelerating: handle it
        if (decelerate == NO) {

            [self deleteCell];
        }
    }

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        [self deleteCell];
    }

    - (void)deleteCell {

        NSIndexPath *pathForCenterCell = [self.tableView indexPathForRowAtPoint:CGPointMake(CGRectGetMidX(self.tableView.bounds), CGRectGetMidY(self.tableView.bounds))]; // if you want middle cell

        NSIndexPath *firstVisibleIndexPath = [[self.tableView indexPathsForVisibleRows] objectAtIndex:0]; // if you want first visible cell

        NSIndexPath *lastVisibleIndexPath = [[self.tableView indexPathsForVisibleRows] objectAtIndex:[self.tableView indexPathsForVisibleRows].count];   // last cell in visible cells



        myCollectionView.beginUpdates() //if you are performing more than one operation use this 
        yourImage.removeObjectAtIndex(myNSIndexPath.row)
            myCollectionView.deleteItems(at: [lastVisibleIndexPath])
        myCollectionView.endUpdates() 


     }

斯威夫特 3.0

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    // if decelerating, let scrollViewDidEndDecelerating: handle it
    if decelerate == false {
        deleteCell()
    }
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    deleteCell()
}

func deleteCell() {
    var pathForCenterCell: IndexPath? = tableView.indexPathForRow(at: CGPoint(x: CGFloat(tableView.bounds.midX), y: CGFloat(tableView.bounds.midY)))
        // if you want middle cell
    var firstVisibleIndexPath: IndexPath? = (tableView.indexPathsForVisibleRows?[0] as? IndexPath)
        // if you want first visible cell
    var lastVisibleIndexPath: IndexPath? = (tableView.indexPathsForVisibleRows?[tableView.indexPathsForVisibleRows?.count] as? IndexPath)
    // last cell in visible cells
    myCollectionView.beginUpdates()()
    //if you are performing more than one operation use this 
    yourImage.removeObjectAtIndex(myNSIndexPath.row)
    myCollectionView.deleteItems
}

快乐编码

于 2017-03-28T04:50:18.597 回答