繁体   English   中英

iOS-水平滑动UICollectionViewCell

[英]iOS - swipe UICollectionViewCell horizontally

我有一个看起来像tableView的UICollectionView,我希望单元格只能水平滑动。

我设法使它们移动,但是问题是我也可以将它们向上移动,并且基本上可以沿任何方向移动它们,例如,当您删除它时,我希望它们像tableViewCell一样滑动。

最后,我希望能够水平滑动屏幕上的某个单元格

我已附上一张图像,该图像显示了收集视图现在的外观以及一个移动的单元格(红色的)

在此处输入图片说明

毕竟,我找到了一种使用UIPanGestureRecognizer进行操作的方法。 所有操作都在单元格上(单元格类内部)。 下面是对我有用的解决方案

var swipeGesture: UIPanGestureRecognizer!
var originalPoint: CGPoint!

func configureCell() {
    setupSwipeGesture()
}

func setupSwipeGesture() {
        swipeGesture = UIPanGestureRecognizer(target: self, action:#selector(swiped(_:)))
        swipeGesture.delegate = self

        self.addGestureRecognizer(swipeGesture)
    }


func swiped(_ gestureRecognizer: UIPanGestureRecognizer) {
let xDistance:CGFloat = gestureRecognizer.translation(in: self).x

        switch(gestureRecognizer.state) {
        case UIGestureRecognizerState.began:
            self.originalPoint = self.center
        case UIGestureRecognizerState.changed:
            let translation: CGPoint = gestureRecognizer.translation(in: self)
            let displacement: CGPoint = CGPoint.init(x: translation.x, y: translation.y)

            if displacement.x + self.originalPoint.x < self.originalPoint.x {
                self.transform = CGAffineTransform.init(translationX: displacement.x, y: 0)
                self.center = CGPoint(x: self.originalPoint.x + xDistance, y: self.originalPoint.y)
            }
        case UIGestureRecognizerState.ended:
            let hasMovedToFarLeft = self.frame.maxX < UIScreen.main.bounds.width / 2
            if (hasMovedToFarLeft) {
                removeViewFromParentWithAnimation()
            } else {
                resetViewPositionAndTransformations()
            }
        default:
            break
        }
    }

func resetViewPositionAndTransformations(){
        UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.0, options: UIViewAnimationOptions(), animations: {
            self.center = self.originalPoint
            self.transform = CGAffineTransform(rotationAngle: 0)
        }, completion: {success in })
    }

func removeViewFromParentWithAnimation() {
        var animations:(()->Void)!
        animations = {self.center.x = -self.frame.width}

        UIView.animate(withDuration: 0.2, animations: animations , completion: {success in self.removeFromSuperview()})
    }

您将需要做一些事情:

1)向每个单元格添加一个UISwipeGestureRecognizer 确保将direction = .left设置direction = .left

2)为手势识别器添加目标,并确保目标可以与集合视图的布局通信。 2a)集合视图数据源可以将目标添加到cellForItemAtIndexPath 您需要一种方法来确保它不会在重复使用的单元格上重复添加。 2b)您可以在单元格中添加手势识别器,并与视图控制器创建委托关系。

3)您将需要被刷过的单元格的索引路径。 要从手势识别器(2a)中获取单元格,请调用其view属性并转换为UICollectionViewCell。 如果使用委托(2b)或其他方法,则应将单元格传回。 然后,您可以调用collectionView.cellForItem(at indexPath: IndexPath) -> UICollectionViewCell? 获取索引路径。

4)在集合视图, performBatchUpdates等中调用deleteItemAtIndexPath:

5)似乎可以用动画的方式简化左边的消失(免责声明,尽管我已经使用过属性,但我自己还没有完成这部分工作)是在UICollectionViewLayout的子类中实现finalLayoutAttributesForDisappearingItemAtIndexPath :。

首先,您需要缓存属性。 因此,覆盖并缓存:

class LeftDisappearLayout : UICollectionViewFLowLayout {

    var cachedAttributes = [NSIndexPath:UICollectionViewAttributes?]()

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        let attributes = super.layoutAttributesForItem(at: indexPath) 
        cachedAttributes[indexPath] = attributes
        return attributes
    }

然后从iOS 9开始,出现了一种简单的方法让系统为您创建左侧动画:

    func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        if var attributes = cachedAttributes[indexPath] {
            let newCenterX : CGFloat = -1 * (attributes.frame.size.width / 2)  
            attributes.center = CGPoint(newCenterX, attributes.center.y)
            return attributes 
        }
        AssertionFailure("Unable to find cached attributes!") // Saying, "This should never happen and in development crash to alert the developer, but in production gracefully revert to default."
        return nil
    }
}

确保将您的收藏夹视图设置为使用此布局。 希望这可以帮助。

暂无
暂无

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

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